繁体   English   中英

Eclipse (JBoss Developer Studio) 不自动构建 JPA 元模型类

[英]Eclipse (JBoss Developer Studio) not automatically building JPA metamodel classes

我有一个 Maven 项目设置,它有一个父模块和两个子模块:

parent
    business
    support

我所有的 JPA 实体都在支持模块中。 当我使用 Criteria API 时,我决定使用 JPA 元模型类来提供类型安全。我对支持模块的 pom.xml 进行了更改(见下文),元模型类正在support/target/metamodel中正确创建support/target/metamodel当我使用mvn clean install从命令行构建时。 构建工作和可部署工件在部署时工作。

问题是,当我按照此处的说明(它反映了许多其他地方)如何设置 Eclipse 来构建元模型类时,Eclipse 似乎没有做任何事情。 它创建了 support/target/metamodel 目录,但里面什么也没有。 我已经从命令行使用mvn clean了 Eclipse 内部,多次完成 Maven->Update Project,但似乎没有任何效果。

我错过了什么?

这是我的支持项目的属性。

在此处输入图像描述

我的支持模块的 pom.xml 的相关部分是:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <compilerArgument>-proc:none</compilerArgument>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <!-- source output directory -->
                        <outputDirectory>target/metamodel</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.12</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>target/metamodel</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

版本信息:

Red Hat JBoss Developer Studio 版本:10.2.0.GA Build id:GA-v20161125-1418-B55 Build date:20161125-1418

Java 1.8.0_112

Maven(命令行):3.3.9

Maven (Eclipse - m2e): 1.7.120161104-1805 嵌入式版本 3.3.9

这是对我有用的设置。

我没有使用 org.bsc.maven:maven-processor-plugin,而是设置了 maven-compiler-plugin 进行注释处理。 说明中提到的问题,即MCOMPILER-62MCOMPILER-66 ,现已关闭,所以我认为没有理由打扰 org.bsc.maven:maven-processor-plugin。

另一个显着的区别是 Eclipse 配置中的“工厂路径”。

pom.xml

开始的最小设置。 注意 maven-compiler-plugin 配置。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>scratch</groupId>
    <artifactId>jpa</artifactId>
    <version>0.1.0-SNAPSHOT</version>

    <properties>
        <version.plugin.maven.compiler>3.5</version.plugin.maven.compiler>
        <version.hibernate>5.2.5.Final</version.hibernate>
    </properties>

    <build>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${version.plugin.maven.compiler}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                    <encoding>UTF-8</encoding>
                    <annotationProcessors>
                        <annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
                    </annotationProcessors>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.hibernate</groupId>
                            <artifactId>hibernate-jpamodelgen</artifactId>
                            <version>${version.hibernate}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${version.hibernate}</version>
        </dependency>
    </dependencies>
</project>

您可能希望手动运行一次或手动下载 hibernate-jpamodelgen(例如mvn dependency:get -Dartifact=org.hibernate:hibernate-jpamodelgen:5.2.5.Final ),以便 JAR 可用于 Eclipse 配置.

日食配置

  1. 转到项目属性(项目上下文菜单 → 属性或 Alt+Enter)
  2. 选择“Java编译器”→“注解处理”
  3. 检查以下内容:
    • “启用项目特定设置”
    • “启用注释处理”
    • “在编辑器中启用处理”
  4. 生成的源目录: target/generated-sources/annotations/ (这是Maven默认放置它们的地方)
  5. 选择“工厂路径”
  6. 从您的 Maven 存储库中添加以下外部 jars( hibernate-jpamodelgen版本可能会有所不同):
    • org/hibernate/hibernate-jpamodelgen/5.2.5.Final/hibernate-jpamodelgen-5.2.5.Final.jar
    • javax/persistence/persistence-api/1.0.2/persistence-api-1.0.2.jar
  7. 将生成的源文件夹(如步骤 4 中配置的)添加到 Java 源文件夹(Java 构建路径 → 源选项卡 → 添加文件夹...)
  8. 之后可能需要对项目进行清理构建

这适用于 eclipse 2019-06 或更高版本:

根据 Jboss 文档,不需要显式设置处理器插件,因为元模型类是在 Maven 构建时在\\target\\generated-sources\\annotations项目文件夹中自动创建的(自 JDK 1.6 起)。

因此,您只需要确保Annotation Processing引用此目标文件夹,如下所示:

在此处输入图片说明

如果您打算在 Java 11 中使用最近的 eclipse,您将遇到即使在 eclipse 的注释处理中指定 hibernate-jpamodelgen 也不会生成 javamodel 的问题。 如果您检查错误选项卡,您将看到: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

这是因为它已在 Java 11 中删除,因此要修复它,您还应该使用 hibernate-jpamodelgen 在工厂路径上添加 jar jaxb-api。

我把这个留在这里给任何可能遇到我遇到的同样问题的人(以及当我再次搜索并忘记这一点时)

解决此问题的最简单方法是执行“mvn clean install”,这将在目标目录中创建元模型,然后使用生成的元模型作为源文件夹,如下所示

在此处输入图像描述

完成上述步骤后,您的项目结构应如下所示

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM