kapt generated code not available during compilation phase
Asked Answered
E

2

10

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?

Eucken answered 15/10, 2018 at 15:1 Comment(2)
Are you using maven > 3.2? (issues.apache.org/jira/browse/MNG-5799)Polyhydroxy
@Polyhydroxy : Yep, maven 3.5.0Eucken
E
5

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.

Eucken answered 30/10, 2018 at 21:44 Comment(0)
M
0

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.

Meagre answered 29/5, 2020 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.