英文:
Java 11-Kotlin annotation processor
问题
我们有一个生成代码的注解处理器。这个注解处理器从2013年开始使用,目前正常工作。然而,我无法使其与 Kotlin 类一起正常工作。
目前在 Java 中的使用方式是:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<annotationProcessors>
<annotationProcessor>db.annotationprocessing.PropertyAnnotationProcessor</annotationProcessor>
</annotationProcessors>
<annotationProcessorPaths>
<dependency>
<groupId>to.etc.domui</groupId>
<artifactId>property-annotations-processor</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
但这不会处理 Kotlin,因为它是 Java 编译器。所以我在此之前使用插件编译了 Kotlin:
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<annotationProcessorArgs>
<processorArg>kapt.kotlin.generated=${project.build.outputDirectory}/kaptStubs</processorArg>
</annotationProcessorArgs>
<sourceDirs>
<sourceDir>src/main/java</sourceDir>
<sourceDir>src/test/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<!-- 在这里指定你的注解处理器。 -->
<annotationProcessorPath>
<groupId>to.etc.domui</groupId>
<artifactId>property-annotations-processor</artifactId>
<version>${domui.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<!-- 其他 execution 配置 -->
</executions>
</plugin>
我已经连接了调试器到注解处理器,并期望在 kotlin-maven-plugin 运行期间命中断点。但实际上并没有发生。在 Java 编译过程中确实发生了,但没有任何关于我创建的 Kotlin 类的引用。
奇怪的是,它在 /target/kaptStubs 下生成了类,IntelliJ 将其解释为 Java 类,而在 target/classes/ 下生成的类是 Kotlin 类。
然而,有一个警告:
[WARNING] 'tools.jar' 未找到,kapt 可能工作不稳定。
环境信息:
java --version
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1, mixed mode, sharing)
Kotlin 类已经编译(我可以在 target/classes 中找到它),但是注解处理器甚至没有被触发。有人有任何想法可能出现问题的地方吗?
英文:
We have an annotation processor that generates code. This annotation processor has been used since 2013, and it works correctly. However, I can't get it to work with Kotlin classes, at all.
Current usage with java is
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<annotationProcessors>
<annotationProcessor>db.annotationprocessing.PropertyAnnotationProcessor</annotationProcessor>
</annotationProcessors>
<annotationProcessorPaths>
<dependency>
<groupId>to.etc.domui</groupId>
<artifactId>property-annotations-processor</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
But this does not process kotlin, because it's a java compiler. So I compiled Kotlin before that, with plugin:
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<annotationProcessorArgs>
<processorArg>
kapt.kotlin.generated=${project.build.outputDirectory}/kaptStubs
</processorArg>
</annotationProcessorArgs>
<sourceDirs>
<sourceDir>src/main/java</sourceDir>
<sourceDir>src/test/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<!-- Specify your annotation processors here. -->
<annotationProcessorPath>
<groupId>to.etc.domui</groupId>
<artifactId>property-annotations-processor</artifactId>
<version>${domui.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
I attached the debugger to the annotation processor and expected the breakpoint to be hit during kotlin-maven-plugin. This didn't happen. It did during java compilation and there isn't any reference of the Kotlin class i made to test this.
Weirdly enough, it generated classes under /target/kaptStubs, which IntellIJ interprets as java class and that one under target/classes/ as Kotlin class.
This is, however, this warning:
[WARNING] 'tools.jar' was not found, kapt may work unreliably.
Environment:
java --version
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1, mixed mode, sharing)
Kotlin class gets compiled (i can find it target/classes),but the annotation processor is not even triggered. Anyone got any ideas where the issue might be?
答案1
得分: 4
由于某种原因,kapt未能检测到META-INF/services/javax.annotation.processing.Processor文件。我认为这是kapt的一个错误,因为这是标准的操作方式。解决方法是通过定义来指定所需的注解处理器:
<annotationProcessors>
<processor>db.annotationprocessing.PropertyAnnotationProcessor</processor>
</annotationProcessors>
这仍然会将生成的代码放入错误的目录,但至少它生成了代码。
完整的执行步骤如下:
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<phase>process-sources</phase>
<configuration>
<sourceDirs>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>to.etc.domui</groupId>
<artifactId>property-annotations-processor</artifactId>
<version>${domui.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<annotationProcessors>
<processor>db.annotationprocessing.PropertyAnnotationProcessor</processor>
</annotationProcessors>
</configuration>
</execution>
英文:
For some reason, kapt is not picking up the
META-INF/services/javax.annotation.processing.Processor
file. I think this is a bug in kapt, since this is standard way of doing things.
The way around it is to specify which annotation processor you want by defining:
<annotationProcessors>
<processor>db.annotationprocessing.PropertyAnnotationProcessor</processor>
</annotationProcessors>
This still generates the code in the wrong directory, but hey, at least it's generating code.
The full execution step is as following:
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<phase>process-sources</phase>
<configuration>
<sourceDirs>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>to.etc.domui</groupId>
<artifactId>property-annotations-processor</artifactId>
<version>${domui.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<annotationProcessors>
<processor>db.annotationprocessing.PropertyAnnotationProcessor</processor>
</annotationProcessors>
</configuration>
</execution>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论