Using kapt generated code in same module


#1

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>
                    <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?


#2

Hello, AFAIK this should work correctly. Is it possible for you to create an issue at http://kotl.in/issue and share a minimal project that demonstrates the problem?


#3

You may want to take a look to this project


#4

Thanks for the reference. However, that’s the gradle plugin, rather than Maven, so I’m not sure it’s a valid comparison.


#5

For anyone who stumbles upon this, Alexey is correct, this does work. My issue was caused by Maven, and having a parent pom also declare a kapt processor, without the ordering correctly specified - which meant that the compile phase got invoked before kapt.

Aligning the parent pom to match the documented solution resolved the issue.