Reputation: 29290
I have written a code generator using kapt, and am using it in a project compiling kotlin with maven.
I find that the kapt generator is invoked after Kotlin's compile phase, which prevents me from using the generated code within kotlin in the same project.
However, if I reference the generated classes from within Java in the same project, it works fine. This is because the java compilation phase comes after kotlin's generation phase.
I've specified the kapt goal before Kotlin's compilation goal within the maven config, (as mentioned in the docs) but it doesn't seem to make a difference:
<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>
<sourceDirs>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>lang.taxi</groupId>
<artifactId>taxi-annotation-processor</artifactId>
<version>${taxi.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<execution>
<id>compile</id>
<goals> <goal>compile</goal> </goals>
</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>
Is it possible to configure Kotlin to allow me to use the generated code from Kotlin in the same project?
Upvotes: 10
Views: 1245
Reputation: 2824
You can use gradle DAG to solve your problem simply by making compile/assemble task depend on kapt task. I know how to do this in android if that help you please let me know and I will post the code right after.
Upvotes: 0
Reputation: 29290
The issue was that the kotlin-maven-plugin
was defined in a parent pom, without the kapt goal, and then again in the module's own pom, with kapt.
This resulted in the compile task being run before the kapt task, even though the module's pom specified the order of kapt
before compile
.
Removing the parent pom entry resolved the issue.
Upvotes: 5