【发布时间】:2020-06-17 04:53:26
【问题描述】:
我有一些 Java 实体类和一些 Kotlin 实体类。我刚刚在我的 pom.xml 中添加了以下内容:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</dependency>
IntelliJ 现在显示 java 实体的元模型类,但不显示 kotlin 实体。所以我加了 kapt 没有效果:
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<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>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<!-- Specify your annotation processors here. -->
<annotationProcessorPath>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
</executions>
<configuration>
<jvmTarget>11</jvmTarget>
<args>
<arg>-Xjvm-default=enable</arg>
<!--<arg>-XXLanguage:+JvmStaticInInterface</arg>-->
</args>
<compilerPlugins>
<plugin>jpa</plugin>
<!-- <plugin>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</plugin>-->
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</dependency>
</dependencies>
</plugin>
我尝试添加<plugin>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</plugin> 没有明显效果。我尝试在 IntelliJ IDEA 中启用注释处理:
哦。我可以在 target/generated-sources/kapt/compile/ 而不是 target/generated-sources/annotations/ 中看到为 Kotlin 类生成的文件。这可能就像设置适当的输出目录一样简单。
现在,当我运行 mvn clean compile 时,我只收到 Java 实体类的错误,说注释已经运行(可能是由 kapt 运行)并创建了类:
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MyProject ---
[INFO] Deleting /myproject/target
[INFO]
[INFO] --- kotlin-maven-plugin:1.3.70:kapt (kapt) @ MyProject ---
[WARNING] 'tools.jar' was not found, kapt may work unreliably
[INFO] Applied plugin: 'jpa'
[INFO] Note: Hibernate JPA 2 Static-Metamodel Generator 5.4.12.Final
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MyProject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 200 resources
[INFO]
[INFO] --- kotlin-maven-plugin:1.3.70:compile (compile) @ MyProject ---
[INFO] Applied plugin: 'jpa'
[WARNING] Duplicate source root: /myproject/target/generated-sources/kapt/compile
[WARNING] Duplicate source root: /myproject/target/generated-sources/kaptKotlin/compile
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (java-compile) @ MyProject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 261 source files to /myproject/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Problem with Filer: Attempt to recreate a file for type my.project.db.SomeJavaClass_
[ERROR] Problem with Filer: Attempt to recreate a file for type my.project.db.AnotherJavaClass_
...
【问题讨论】:
-
你的问题解决了吗?
-
没有。我不知道如何设置输出目录。我也不知道这是否真的能解决它。
-
您不必设置输出目录。这应该由编译器使用。使用 Maven 编译或仅在 Eclipse 中编译有问题吗?
-
Maven 和 IntelliJ(不是 Eclipse)中的问题。
标签: hibernate maven kotlin annotation-processing kapt