Maven dependency declaration triggers changes in compilation procedure?
Asked Answered
U

1

5

So I have an application that uses both, Java and Kotlin sourcefiles (all placed in the /src/main/kotlin directory because we eventually want to migrate to kotlin anyway) and that generates an hibernate metamodel.

So our maven compile plugins look like this:

<build>
    <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
    <finalName>${project.artifactId}</finalName>
    <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!--COMPILATION-->
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>kapt</id>
                        <goals>
                            <goal>kapt</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>src/main/kotlin</sourceDir>
                            </sourceDirs>
                            <annotationProcessorPaths>
                                <annotationProcessorPath>
                                    <groupId>org.hibernate</groupId>
                                    <artifactId>hibernate-jpamodelgen</artifactId>
                                    <version>${hibernate.version}</version>
                                </annotationProcessorPath>
                            </annotationProcessorPaths>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <jvmTarget>1.8</jvmTarget>

                    <compilerPlugins>
                        <plugin>all-open</plugin>
                        <plugin>jpa</plugin>
                        <plugin>spring</plugin>
                        <plugin>no-arg</plugin>
                    </compilerPlugins>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>

                    <pluginOptions>
                        <!-- Each annotation is placed on its own line -->
                        <option>all-open:annotation=javax.ejb.Stateless</option>
                        <option>no-arg:annotation=javax.ejb.Stateless</option>

                        <option>all-open:annotation=javax.ws.rs.ext.Provider</option>
                        <option>no-arg:annotation=javax.ws.rs.ext.Provider</option>

                    </pluginOptions>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-noarg</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <!-- Check https://kotlinlang.org/docs/reference/using-maven.html#compiling-kotlin-and-java-sources -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <executions>
                    <!-- Replacing default-compile as it is treated specially by maven -->
                    <execution>
                        <id>default-compile</id>
                        <phase>none</phase>
                    </execution>
                    <!-- Replacing default-testCompile as it is treated specially by maven -->
                    <execution>
                        <id>default-testCompile</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>java-compile</id>
                        <phase>compile</phase>
                        <goals> <goal>compile</goal> </goals>
                    </execution>
                    <execution>
                        <id>java-test-compile</id>
                        <phase>test-compile</phase>
                        <goals> <goal>testCompile</goal> </goals>
                    </execution>
                </executions>
            </plugin>
            <!--END COMPILATION-->

            <!-- ... -->    
    </plugins>
</build>

this results in

[WARNING] Duplicate source root: /home/cypherk/code/myapp/target/generated-sources/kapt/compile
[WARNING] Duplicate source root: /home/cypherk/code/myapp/target/generated-sources/kaptKotlin/compile

which I have no idea why but may be related.

If I do not declare a dependency to

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>${hibernate.version}</version>
</dependency>

under <dependencies>, that's alright, everything gets generated the way it's supposed to get generated in /target/generated-sources/kapt/compile/. (/target/generated-sources-kaptKotlin/compile gets generated but remains empty).

However, if I do declare the dependency under <dependencies>, the java (but not kotlin) entities will get generated a second time in /target/generated-sources/annotations/, which will trigger a compilation error because all Java-based generated classes have a duplicate class in the kapt folder.

I'm no expert on Maven, I just use it because that's what we are supposed to use for the project. As such, I find simply declaring a dependency having such an effect exceedingly unintuitive.

Could anybody explain to me why this is happening?

Unscramble answered 17/4, 2019 at 12:22 Comment(0)
E
11

I don't know why, but when you change the compile execution to:

<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
    <goal>compile</goal>
</goals>
<configuration>
    <sourceDirs>
        <sourceDir>src/main/kotlin</sourceDir>
    </sourceDirs>
</configuration>
</execution>

It should work. Same with test-compile.

Efface answered 3/7, 2019 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.