Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kapt generated code not available during compilation phase

Tags:

maven

kotlin

kapt

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?

like image 687
Marty Pitt Avatar asked Oct 15 '18 15:10

Marty Pitt


People also ask

What is Kapt annotation in Java?

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.

How to generate Kotlin source files using Kapt?

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.

Can I use annotation processors in Kotlin projects?

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.

What is the Kapt configuration for androidtest?

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.


1 Answers

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.

like image 177
Marty Pitt Avatar answered Oct 06 '22 00:10

Marty Pitt