[英]Generating metamodel classes using maven and eclipse (JPA,Maven,Eclipse)
[英]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-62和MCOMPILER-66 ,现已关闭,所以我认为没有理由打扰 org.bsc.maven:maven-processor-plugin。
另一个显着的区别是 Eclipse 配置中的“工厂路径”。
开始的最小设置。 注意 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 配置.
target/generated-sources/annotations/
(这是Maven默认放置它们的地方)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
如果您打算在 Java 11 中使用最近的 eclipse,您将遇到即使在 eclipse 的注释处理中指定 hibernate-jpamodelgen 也不会生成 javamodel 的问题。 如果您检查错误选项卡,您将看到: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
这是因为它已在 Java 11 中删除,因此要修复它,您还应该使用 hibernate-jpamodelgen 在工厂路径上添加 jar jaxb-api。
我把这个留在这里给任何可能遇到我遇到的同样问题的人(以及当我再次搜索并忘记这一点时)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.