2-staged compilation of Kotlin code with kotlin-maven-plugin

122 views Asked by At

I have a Kotlin maven-based project, compiled with kotlin-maven-plugin. My project structure looks like following:

<root>
+- src/gen/kotlin
+- src/main/kotlin
...
  • src/gen/kotlin contains sources that will generate other source files.
  • src/main/kotlin contains main project sources.

I want to run a 2-staged compilation during maven build:

  1. compile only the generator sources from src/gen/kotlin, presumably bound to process-sources phase. I will then use maven-exec-plugin to execute the compiled generator.
  2. compile everything from src/main/kotlin + the output of the generator.

I tried to create 2 separate executions inside kotlin-maven-plugin:

...
<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version>
    <configuration>
        <compilerPlugins>
        ...
        </compilerPlugins>
    </configuration>
    <executions>
        <execution>
            <id>codegen</id>
            <goals>
                <goal>compile</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration >
                <sourceDirs>
                    <sourceDir>${project.basedir}/src/gen/kotlin</sourceDir>
                </sourceDirs>
            </configuration>
        </execution>
        <execution>
            <id>compile</id>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
    ...
</plugin>

The trouble is that my parent pom defines

<build>
    <sourceDirectory>src/main/kotlin</sourceDirectory>
    <testSourceDirectory>src/test/kotlin</testSourceDirectory>
    ...
</build>

This build.sourceDirectory then gets appended to my codegen execution even though I explicitly configure src/gen/kotlin as a sourceDir:

> mvn -X clean process-sources
...
[DEBUG] Loading mojo org.jetbrains.kotlin:kotlin-maven-plugin:1.8.22:compile from plugin realm ClassRealm[plugin>org.jetbrains.kotlin:kotlin-maven-plugin:1.8.22, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@42110406]
[DEBUG] Configuring mojo execution 'org.jetbrains.kotlin:kotlin-maven-plugin:1.8.22:compile:codegen' with basic configurator -->
[DEBUG]   (f) classpath = ...
...
[DEBUG]   (f) output = <path>/target/classes
...
[DEBUG]   (f) sourceDirs = [<path>/src/gen/kotlin]
...
[DEBUG] Compiling Kotlin sources from [<path>/src/gen/kotlin, <path</src/main/kotlin]

Is there a way to achieve my desired 2-staged compilation without having to go with submodules? Maybe there's a way to ignore / exclude the default build source directory for a particular execution?

0

There are 0 answers