所以我有一个应用程序,它同时使用 Java 和 Kotlin 源文件(全部放在 /src/main/kotlin
目录中,因为我们最终还是想迁移到 kotlin)并生成一个 hibernate 元模型。
所以我们的 Maven 编译插件看起来像这样:
<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>
这会导致
[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
我不知道为什么,但可能有关系。
如果我不声明对
的依赖<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</dependency>
在<dependencies>
下,没关系,一切都会按照 /target/generated-sources/kapt/compile/
中应该生成的方式生成。 。 ( /target/generated-sources-kaptKotlin/compile
生成但保持为空)。
但是,如果我确实在 <dependencies>
下声明依赖项,java(但不是 kotlin)实体将在 /target/generated-sources/annotations/
中生成第二次,这将触发编译错误,因为所有基于 Java 生成的类都有 duplicate class
在 kapt 文件夹中。
我不是 Maven 方面的专家,我只是使用它,因为这就是我们应该在项目中使用的东西。因此,我发现简单地声明具有这种效果的依赖项非常不直观。
有人可以向我解释一下为什么会发生这种情况吗?
最佳答案
我不知道为什么,但是当你将编译执行更改为:
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
它应该可以工作。与测试编译相同。
关于java - Maven依赖声明触发编译过程的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55727473/