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

时间:2016-12-21 19:16:09

标签: java eclipse maven jpa metamodel

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

parent
    business
    support

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

问题在于,当我按照here(反映许多其他地方)关于如何设置Eclipse来构建元模型类的说明时,Eclipse似乎没有做任何事情。它创建了support / target / metamodel目录,但其中没有任何内容。我在Eclipse中使用mvn clean从命令行清理,多次完成Maven-> Update Project但似乎没有任何工作。

我错过了什么?

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

enter image description here

我的支持模块的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>

版本信息:

红帽JBoss开发者工作室 版本:10.2.0.GA 构建ID:GA-v20161125-1418-B55 建设日期:20161125-1418

Java 1.8.0_112

Maven(命令行):3.3.9

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

3 个答案:

答案 0 :(得分:2)

这是适用于我的设置。

我没有使用org.bsc.maven:maven-processor-plugin,而是设置maven-compiler-plugin进行注释处理。 instructions中提到的问题,即MCOMPILER-62MCOMPILER-66现已关闭,因此我认为没有理由为org.bsc.maven:maven-processor-plugin而烦恼。

另一个值得注意的区别是Eclipse配置中的“Factory Path”。

的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配置。

Eclipse配置

  1. 转到项目属性(项目上下文菜单→属性或Alt + Enter)
  2. 选择“Java编译器”→“注释处理”
  3. 检查以下内容:
    • “启用项目特定设置”
    • “启用注释处理”
    • “在编辑器中启用处理”
  4. 生成的源目录:target/generated-sources/annotations/ (这是Maven默认放置的地方)
  5. 选择“工厂路径”
  6. 从Maven存储库添加以下外部jar(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. 之后可能需要清理构建项目

答案 1 :(得分:1)

此功能适用于Eclipse 2019-06或更高版本:

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

因此,您只需要确保注释处理引用了此目标文件夹,如下所示:

enter image description here

答案 2 :(得分:0)

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

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

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