4

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?

2
  • This is a not related to your question. But, I recommend using Gradle instead of Maven. When I used to use Maven for Kotlin project, I had many problems with it. After moved to Gradle, there was no problem. Commented Jul 29, 2020 at 5:13
  • Thanks, but I can't change it now... It's really a lot of work to do so. Commented Jul 29, 2020 at 7:02

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

It may be intentionally unsupported. Because adding this one line is a one-time thing and is obvious from Kapt docs. But scanning all jars (which may go to thousands) for the presence of the file on each builds (which may go to thousands) is, if not anything else, a waste of time and electricity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.