4

We use an annotation processing framework (Immutables) to generate Java classes from interfaces.

Now I have to access these generated classes from a kotlin class. While in Java this works well, the Kotlin compiler cannot find them.

Here is the maven configuration:

        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/main/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/test/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <!-- Replacing default-compile as it is treated specially by maven -->
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
                <!-- Replacing default-testCompile as it is treated specially by maven -->
                <execution>
                    <id>default-testCompile</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <id>java-compile</id>
                    <phase>compile</phase>
                    <goals> <goal>compile</goal> </goals>
                </execution>
                <execution>
                    <id>java-test-compile</id>
                    <phase>test-compile</phase>
                    <goals> <goal>testCompile</goal> </goals>
                </execution>
            </executions>
        </plugin>

Kotlin version is 1.2.10 (but it's the same with 1.2.20). Swapping the plugin declarations did not help. How can I configure this?

Marc von Renteln
  • 1,229
  • 15
  • 34

1 Answers1

6

The Kotlin compiler runs before the Java compiler. To have the created classes available with Kotlin, you have to use kapt and define every annotation processor there.

<execution>
    <id>kapt</id>
    <goals>
        <goal>kapt</goal>
    </goals>
    <configuration>
        <sourceDirs>
            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
            <sourceDir>${project.basedir}/src/main/java</sourceDir>
        </sourceDirs>
        <annotationProcessorPaths>
            <annotationProcessorPath>
                <groupId>org.immutables</groupId>
                <artifactId>value</artifactId>
                <version>2.5.5</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</execution>
tynn
  • 38,113
  • 8
  • 108
  • 143
  • kapt is executed and finds the immutables jar. But the classes get not generated and the compiler still fails... ```[INFO] [kapt] Kapt3 is enabled. [INFO] [kapt] Annotation processing mode: STUBS_AND_APT [INFO] [kapt] Use light analysis: true [INFO] [kapt] Correct error types: false [INFO] [kapt] Source output directory: /target/generated-sources/kapt/compile [INFO] [kapt] Classes output directory: /target/classes [INFO] [kapt] Stubs output directory: /target/kaptStubs/compile [INFO] [kapt] Incremental data output directory: null``` – Marc von Renteln Feb 06 '18 at 14:16
  • Is kapt executing annotation processors for Java files or only for Kotlin files? The annotated classes are still Java classes. – Marc von Renteln Feb 06 '18 at 14:28
  • In [Android context](https://kotlinlang.org/docs/reference/kapt.html#using-in-gradle): If your project contains Java classes, kapt will also take care of them. Did you find generated classes or stubs in the given directories? – tynn Feb 06 '18 at 14:58
  • Okay, it works exept for two interfaces, that extend other simple interfaces (that have no immutables configuration in it). It seems kapt has a problem to generate these. With java annotation processing this was no problem... – Marc von Renteln Feb 07 '18 at 11:32
  • kapt generation now works. Now there is another problem: Java also tries to generate the classes and the java compiler step fails with a "duplicate class error"... – Marc von Renteln Feb 08 '18 at 10:33
  • If you have the classes available already, just remove the annotation processor from your java compile step. Kapt should be sufficient. – tynn Feb 08 '18 at 10:52
  • Annotation processing is enabled and discovered by default in the maven-compiler-plugin. I had to deactivate it in its configuration using `none`. Now some classes are missing that kapt could not generate (in my comment above). I have to investigate further to find what causes this error. Thanks. – Marc von Renteln Feb 08 '18 at 14:47
  • 2
    Solved. I found a strange bug in kapt in combination with immutables: We use custom annotations for our immutables. These have to be configured in a file `src/main/resources/META-INF/annotations/org.immutables.value.immutable`. But all classes with custom annotations were not generated exept those where the custom annotations were provided by a maven project which this project depends on. If I add the custom annotations to the file in the other project it works. *This means kapt does not use the file in the given src/main/java folder but only that one on the classpath!* – Marc von Renteln Feb 08 '18 at 15:29