简体   繁体   English

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

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

I have a Maven project set up that has a parent and two child modules:我有一个 Maven 项目设置,它有一个父模块和两个子模块:

parent
    business
    support

All of my JPA entities are in the support module.我所有的 JPA 实体都在支持模块中。 I've decided I want to use JPA metamodel classes to provide type safety when I use the Criteria API. I made changes to the pom.xml for the support module (see below) and the metamodel classes are being created correctly in support/target/metamodel when I build from the command line using mvn clean install .当我使用 Criteria API 时,我决定使用 JPA 元模型类来提供类型安全。我对支持模块的 pom.xml 进行了更改(见下文),元模型类正在support/target/metamodel中正确创建support/target/metamodel当我使用mvn clean install从命令行构建时。 Builds work and deployable artifacts work when deployed.构建工作和可部署工件在部署时工作。

The issue is that when I follow the instructions here (which mirrors many other places) on how to set up Eclipse to build the metamodel classes, Eclipse doesn't seem to do anything.问题是,当我按照此处的说明(它反映了许多其他地方)如何设置 Eclipse 来构建元模型类时,Eclipse 似乎没有做任何事情。 It creates the support/target/metamodel directory but there's never anything in it.它创建了 support/target/metamodel 目录,但里面什么也没有。 I've cleaned inside Eclipse, from the command line using mvn clean , done Maven->Update Project multiple times but nothing seems to work.我已经从命令行使用mvn clean了 Eclipse 内部,多次完成 Maven->Update Project,但似乎没有任何效果。

What am I missing?我错过了什么?

Here's what my support project's properties look like.这是我的支持项目的属性。

在此处输入图像描述

The relavant section of my support module's pom.xml is:我的支持模块的 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>

Version information:版本信息:

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

Java 1.8.0_112 Java 1.8.0_112

Maven (command line): 3.3.9 Maven(命令行):3.3.9

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

This is the setup that has worked for me.这是对我有用的设置。

I did not use the org.bsc.maven:maven-processor-plugin, rather set up the maven-compiler-plugin for annotation processing.我没有使用 org.bsc.maven:maven-processor-plugin,而是设置了 maven-compiler-plugin 进行注释处理。 The issues mentioned in the instructions , ie MCOMPILER-62 and MCOMPILER-66 , are now closed, so I see no reason why to bother with the org.bsc.maven:maven-processor-plugin. 说明中提到的问题,即MCOMPILER-62MCOMPILER-66 ,现已关闭,所以我认为没有理由打扰 org.bsc.maven:maven-processor-plugin。

Another notable difference is the "Factory Path", in the Eclipse configuration.另一个显着的区别是 Eclipse 配置中的“工厂路径”。

pom.xml pom.xml

A minimal setup to get started.开始的最小设置。 Note the maven-compiler-plugin configuration.注意 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>

You may want to run it once by hand or download hibernate-jpamodelgen by hand (eg mvn dependency:get -Dartifact=org.hibernate:hibernate-jpamodelgen:5.2.5.Final ), so that the JAR is available for the Eclipse configuration.您可能希望手动运行一次或手动下载 hibernate-jpamodelgen(例如mvn dependency:get -Dartifact=org.hibernate:hibernate-jpamodelgen:5.2.5.Final ),以便 JAR 可用于 Eclipse 配置.

Eclipse configuration日食配置

  1. Go to project properties (project context menu → Properties or Alt+Enter)转到项目属性(项目上下文菜单 → 属性或 Alt+Enter)
  2. Select "Java Compiler" → "Annotation Processing"选择“Java编译器”→“注解处理”
  3. Check the following:检查以下内容:
    • "Enable project specific settings" “启用项目特定设置”
    • "Enable annotation processing" “启用注释处理”
    • "Enable processing in editor" “在编辑器中启用处理”
  4. Generated source directory: target/generated-sources/annotations/ (this is where Maven puts them by default)生成的源目录: target/generated-sources/annotations/ (这是Maven默认放置它们的地方)
  5. Select "Factory Path"选择“工厂路径”
  6. Add the following external jars, from your Maven repository ( hibernate-jpamodelgen version will probably vary):从您的 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. Add the generated sources folder (as configured in step 4) to the Java source folders (Java build path → Source tab → Add Folder...)将生成的源文件夹(如步骤 4 中配置的)添加到 Java 源文件夹(Java 构建路径 → 源选项卡 → 添加文件夹...)
  8. A clean-build of the project may be required afterwards之后可能需要对项目进行清理构建

This will work for eclipse 2019-06 or later versions:这适用于 eclipse 2019-06 或更高版本:

According to Jboss documentation, it is not required to explicitly set the processor plugin, since the metamodel classes are created automatically on Maven build in \\target\\generated-sources\\annotations project's folder (since JDK 1.6).根据 Jboss 文档,不需要显式设置处理器插件,因为元模型类是在 Maven 构建时在\\target\\generated-sources\\annotations项目文件夹中自动创建的(自 JDK 1.6 起)。

Thus, you only need to make sure the Annotation Processing refers to this target folder, as in here:因此,您只需要确保Annotation Processing引用此目标文件夹,如下所示:

在此处输入图片说明

in case you are going to use a recent eclipse with Java 11, you will meet the issue that the javamodel is not generated even specifying the hibernate-jpamodelgen in annotation processing of eclipse.如果您打算在 Java 11 中使用最近的 eclipse,您将遇到即使在 eclipse 的注释处理中指定 hibernate-jpamodelgen 也不会生成 javamodel 的问题。 If you check the error tabs you will see this: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException如果您检查错误选项卡,您将看到: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

this is due the fact that it has been removed in Java 11, so to fix it you should add also the jar jaxb-api on the factory path with hibernate-jpamodelgen.这是因为它已在 Java 11 中删除,因此要修复它,您还应该使用 hibernate-jpamodelgen 在工厂路径上添加 jar jaxb-api。

I leave this here for anyone that could meet the same problems I had (and for me when I search again and have forgot about this)我把这个留在这里给任何可能遇到我遇到的同样问题的人(以及当我再次搜索并忘记这一点时)

The easiest way to fix this issue is to do a "mvn clean install", which will create the metamodel in the target directory, then use the metamodel generated as the source folder as below解决此问题的最简单方法是执行“mvn clean install”,这将在目标目录中创建元模型,然后使用生成的元模型作为源文件夹,如下所示

在此处输入图像描述

After you do the above step, your project structure should look like below完成上述步骤后,您的项目结构应如下所示

在此处输入图像描述

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

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