【问题标题】:Hibernate Metamodel annotation processor doesn't recognize Kotlin entity classesHibernate Metamodel 注释处理器无法识别 Kotlin 实体类
【发布时间】: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>

我尝试添加&lt;plugin&gt;org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor&lt;/plugin&gt; 没有明显效果。我尝试在 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


【解决方案1】:

我删除了项目对 hibernate-jpamodelgen 和 Kotlin 插件的依赖项,只在 kapt 的 annotationProcessorPath 中提及。 Maven 成功完成。

<!-- Java has a fit if Kapt processes this first,
     so just let Kapt do it and don't tell Java about it. -->
<!--        <dependency>-->
<!--            <groupId>org.hibernate</groupId>-->
<!--            <artifactId>hibernate-jpamodelgen</artifactId>-->
<!--            <version>${hibernate.version}</version>-->
<!--        </dependency>-->

我还回到了 IntelliJ 设置窗口,删除了 JPAMetaModelEntityProcessor 并取消选中“注释处理”。 IntelliJ 现在似乎也很高兴。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2017-05-22
    相关资源
    最近更新 更多