Using kapt generated code in same module


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:

                <goals> <goal>compile</goal> </goals>
                <goals> <goal>test-compile</goal> </goals>

Is it possible to configure Kotlin to allow me to use the generated code from Kotlin in the same project?


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


You may want to take a look to this project


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


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.