[英]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.