Stefa
Stefa

Reputation: 637

Java 11-Kotlin annotation processor

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>

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?

Upvotes: 4

Views: 4649

Answers (1)

Stefa
Stefa

Reputation: 637

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>

Upvotes: 4

Related Questions