Java 11-Kotlin 注解处理器

huangapple go评论77阅读模式
英文:

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

&lt;plugins&gt;
    &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;version&gt;${maven-compiler-plugin.version}&lt;/version&gt;
        &lt;configuration&gt;
            &lt;annotationProcessors&gt;
                &lt;annotationProcessor&gt;db.annotationprocessing.PropertyAnnotationProcessor&lt;/annotationProcessor&gt;
            &lt;/annotationProcessors&gt;
            &lt;annotationProcessorPaths&gt;
                &lt;dependency&gt;
                    &lt;groupId&gt;to.etc.domui&lt;/groupId&gt;
                    &lt;artifactId&gt;property-annotations-processor&lt;/artifactId&gt;
                    &lt;version&gt;1.2-SNAPSHOT&lt;/version&gt;
                &lt;/dependency&gt;
            &lt;/annotationProcessorPaths&gt;
        &lt;/configuration&gt;
    &lt;/plugin&gt;
&lt;/plugins&gt;

</build>

But this does not process kotlin, because it's a java compiler. So I compiled Kotlin before that, with plugin:

&lt;plugin&gt;
			&lt;artifactId&gt;kotlin-maven-plugin&lt;/artifactId&gt;
			&lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
			&lt;version&gt;${kotlin.version}&lt;/version&gt;
			&lt;executions&gt;
				&lt;execution&gt;
					&lt;id&gt;kapt&lt;/id&gt;
					&lt;goals&gt;
						&lt;goal&gt;kapt&lt;/goal&gt;
					&lt;/goals&gt;
					&lt;configuration&gt;
						&lt;annotationProcessorArgs&gt;
							&lt;processorArg&gt;
								kapt.kotlin.generated=${project.build.outputDirectory}/kaptStubs
							&lt;/processorArg&gt;
						&lt;/annotationProcessorArgs&gt;
						&lt;sourceDirs&gt;
							&lt;sourceDir&gt;src/main/java&lt;/sourceDir&gt;
							&lt;sourceDir&gt;src/test/java&lt;/sourceDir&gt;
						&lt;/sourceDirs&gt;
						&lt;annotationProcessorPaths&gt;
							&lt;!-- Specify your annotation processors here. --&gt;
							&lt;annotationProcessorPath&gt;
								&lt;groupId&gt;to.etc.domui&lt;/groupId&gt;
								&lt;artifactId&gt;property-annotations-processor&lt;/artifactId&gt;
								&lt;version&gt;${domui.version}&lt;/version&gt;
							&lt;/annotationProcessorPath&gt;
						&lt;/annotationProcessorPaths&gt;
					&lt;/configuration&gt;
				&lt;/execution&gt;
				&lt;execution&gt;
					&lt;id&gt;compile&lt;/id&gt;
					&lt;goals&gt;
						&lt;goal&gt;compile&lt;/goal&gt;
					&lt;/goals&gt;
					&lt;configuration&gt;
						&lt;sourceDirs&gt;
							&lt;sourceDir&gt;${project.basedir}/src/main/java&lt;/sourceDir&gt;
						&lt;/sourceDirs&gt;
					&lt;/configuration&gt;
				&lt;/execution&gt;
				&lt;execution&gt;
					&lt;id&gt;test-compile&lt;/id&gt;
					&lt;goals&gt;
						&lt;goal&gt;test-compile&lt;/goal&gt;
					&lt;/goals&gt;
					&lt;configuration&gt;
						&lt;sourceDirs&gt;
							&lt;sourceDir&gt;${project.basedir}/src/test/kotlin&lt;/sourceDir&gt;
							&lt;sourceDir&gt;${project.basedir}/src/test/java&lt;/sourceDir&gt;
						&lt;/sourceDirs&gt;
					&lt;/configuration&gt;
				&lt;/execution&gt;
			&lt;/executions&gt;
		&lt;/plugin&gt;

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:

	&lt;annotationProcessors&gt;
		&lt;processor&gt;db.annotationprocessing.PropertyAnnotationProcessor&lt;/processor&gt;
	&lt;/annotationProcessors&gt;

This still generates the code in the wrong directory, but hey, at least it's generating code.

The full execution step is as following:

&lt;execution&gt;
					&lt;id&gt;kapt&lt;/id&gt;
					&lt;goals&gt;
						&lt;goal&gt;kapt&lt;/goal&gt;
					&lt;/goals&gt;
					&lt;phase&gt;process-sources&lt;/phase&gt;
					&lt;configuration&gt;
						&lt;sourceDirs&gt;
							&lt;sourceDir&gt;src/main/java&lt;/sourceDir&gt;
						&lt;/sourceDirs&gt;
						&lt;annotationProcessorPaths&gt;
							&lt;annotationProcessorPath&gt;
								&lt;groupId&gt;to.etc.domui&lt;/groupId&gt;
								&lt;artifactId&gt;property-annotations-processor&lt;/artifactId&gt;
								&lt;version&gt;${domui.version}&lt;/version&gt;
							&lt;/annotationProcessorPath&gt;
						&lt;/annotationProcessorPaths&gt;
						&lt;annotationProcessors&gt;
							&lt;processor&gt;db.annotationprocessing.PropertyAnnotationProcessor&lt;/processor&gt;
						&lt;/annotationProcessors&gt;
					&lt;/configuration&gt;
				&lt;/execution&gt;

huangapple
  • 本文由 发表于 2020年7月28日 22:15:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63136193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定