Java注解处理器编译时未执行问题排查求助
注解处理器未被Gradle识别导致Fixture类无法生成的排查方案
问题背景
我正在实现一个基于注解处理的类库,可在编译时自动生成Fixture类。项目采用单仓库结构:
|-- fixturize | |--fixturize-core |--fixturize-playground
fixturize-core中同时定义了@Fixture注解及对应的注解处理器FixtureProcessor,使用AutoService生成META-INF/services/javax.annotation.processing.Processor文件,该文件已确认存在于编译后的类文件中。
fixturize-core的build.gradle配置
plugins { id "java" id "io.freefair.lombok" version "8.6" } ext { nashornVersion = "15.4" guavaVersion = "33.2.1-jre" autoServiceVersion = "1.1.1" junitVersion = "5.10.0" assertJVersion = "3.25.1" mockitoVersion = "5.12.0" compileTestingVersion = "0.21.0" lombokVersion = "1.18.34" } dependencies { implementation "com.google.auto.service:auto-service:$autoServiceVersion" annotationProcessor "com.google.auto.service:auto-service:$autoServiceVersion" implementation "com.google.guava:guava:$guavaVersion" implementation "org.openjdk.nashorn:nashorn-core:$nashornVersion" testImplementation platform("org.junit:junit-bom:$junitVersion") testImplementation "org.junit.jupiter:junit-jupiter" testImplementation "org.assertj:assertj-core:$assertJVersion" testImplementation "org.mockito:mockito-junit-jupiter:$mockitoVersion" testImplementation "com.google.testing.compile:compile-testing:$compileTestingVersion" testImplementation "org.projectlombok:lombok:$lombokVersion" } test { useJUnitPlatform() }
fixturize-playground的build.gradle配置
plugins { id "java" id "io.freefair.lombok" version "8.6" } tasks.withType(JavaCompile) { doFirst { println "AnnotationProcessorPath for $name is ${options.getAnnotationProcessorPath().getFiles()}" } } compileJava { options.annotationProcessorPath = configurations.annotationProcessor } dependencies { implementation project(":fixturize-core") annotationProcessor project(":fixturize-core") testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation 'org.junit.jupiter:junit-jupiter' } test { useJUnitPlatform() }
测试用标注类
import de.floydkretschmar.fixturize.annotations.Fixture; @Fixture public class Test { private int field; }
执行./gradlew clean build时,未生成目标Fixture类,编译日志显示自定义注解处理器不在注解处理器路径中。
排查与解决方案
1. 确保AutoService配置文件被正确打包
检查fixturize-core/build/libs/fixturize-core-*.jar,确认META-INF/services/javax.annotation.processing.Processor文件存在,且内容为处理器的全类名(如de.floydkretschmar.fixturize.processors.FixtureProcessor)。
若文件缺失,在fixturize-core的build.gradle中添加打包配置:
jar { from sourceSets.main.output.resourcesDir from sourceSets.main.output.classesDirs }
2. 移除手动设置的注解处理器路径
Gradle默认会自动管理annotationProcessor配置的依赖路径,手动设置可能覆盖默认逻辑。删除fixturize-playground中以下配置:
compileJava { options.annotationProcessorPath = configurations.annotationProcessor }
3. 直接引用fixturize-core的打包jar作为注解处理器
修改fixturize-playground的依赖配置,确保使用包含完整资源的jar文件:
dependencies { implementation project(":fixturize-core") annotationProcessor files(project(":fixturize-core").jar.archiveFile) }
4. 验证处理器类的可见性与注解配置
- 确保
FixtureProcessor类为public修饰 - 检查
@SupportedAnnotationTypes中的注解全类名与@Fixture注解的实际全类名完全一致(包括包名)
5. 清理缓存后重新构建
执行以下命令彻底清除缓存并重新构建:
./gradlew clean build --no-build-cache
内容的提问来源于stack exchange,提问作者floydk
相关产品推荐
相关产品推荐

