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?
kapt uses Java compiler to run annotation processors. Some annotation processors (such as AutoFactory) rely on precise types in declaration signatures. By default, kapt replaces every unknown type (including types for the generated classes) to NonExistentClass, but you can change this behavior.
Generating Kotlin sources kapt can generate Kotlin sources. Just write the generated Kotlin source files to the directory specified by processingEnv.options ["kapt.kotlin.generated"], and these files will be compiled together with the main sources. Note that kapt does not support multiple rounds for the generated Kotlin files.
Annotation processors (see JSR 269) are supported in Kotlin with the kapt compiler plugin. In a nutshell, you can use libraries such as Dagger or Data Binding in your Kotlin projects. Please read below about how to apply the kapt plugin to your Gradle/Maven build.
If you use annotation processors for your androidTest or test sources, the respective kapt configurations are named kaptAndroidTest and kaptTest. Note that kaptAndroidTest and kaptTest extends kapt, so you can just provide the kapt dependency and it will be available both for production sources and tests.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With