我正在尝试使用maven-processor-plugin生成JPA元模型java文件,并设置我的pom.xml如下所示。
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<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>${basedir}/src/main/java</outputDirectory>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
实际上,我想生成元模型文件(Entity_.java)到相应实体(Entity.java)的相同包中。因此,我在插件中设置了outputDirectory
<outputDirectory>${basedir}/src/main/java</outputDirectory>
第一次运行是正常的,但是从以后执行元模型java文件重新生成时,插件总是会出现关于文件复制的错误。
我的问题是 - 有没有办法配置插件,以便它可以在重新生成期间覆盖现有文件?
事实上,要解决
这两个都非常不方便,我希望你们能给我一个合适的解决方案。
答案 0 :(得分:8)
正确的解决方案是生成的源应该位于目标文件夹中,不应该放入源文件夹或SCM系统。
当然,通过将生成的源放入目标,您将在IDE中遇到问题,因为无法找到相关代码。因此,您可以添加build-helper-maven-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>${project.build.directory}/generated-sources/java/jpametamodel</outputDirectory>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/java/jpametamodel</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
答案 1 :(得分:7)
如https://docs.jboss.org/hibernate/orm/5.0/topical/html/metamodelgen/MetamodelGenerator.html所述:
只需将其添加到<project> <dependencies> ... </dependencies> </project>
:
pom.xml
即可
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-jpamodelgen -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.2.16.Final</version>
<scope>provided</scope>
</dependency>
您无需做任何其他事情。如果您遇到问题,请访问此答案顶部的jboss页面。
此代码段中包含的版本是截至2018年3月的最新版本,但请检查工件页面(https://mvnrepository.com/artifact/org.hibernate/hibernate-jpamodelgen)以获取最新版本。
这不是一个原始的答案,但应该对那些想要一个简单,直接的可复制的解决方案的人有所帮助。
答案 2 :(得分:2)
如果您在编译期间使用了另一个注释处理器(如 lombok),您可能会遇到以下错误(尽管将 hibernate-jpamodelgen 添加为依赖项):
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /your/not/CompilableClass:[5,68] cannot find symbol
symbol: class YouEntity_
location: package your.not
[INFO] 1 error
在这种情况下,您需要直接为编译器插件声明两个注释处理器(对于 lombok 和 hibernate):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-jpamodelgen.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
答案 3 :(得分:0)
我还有一个有效的解决方案,我想与其他人分享这个。所以我希望这是正确的地方。
可以在GitHub https://github.com/StefanHeimberg/jpa21-maven-metagen
找到代码功能强>
<强>的pom.xml:强>
<?xml version="1.0" encoding="UTF-8"?>
<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>com.github.stefanheimberg</groupId>
<artifactId>jpa21-maven-metagen</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>hibernate</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<hibernate.version>5.0.3.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>eclipselink</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<eclipselink.version>2.6.1</eclipselink.version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>${eclipselink.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>m2e</id>
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
build-helper-maven-plugin
</artifactId>
<versionRange>
[1.9.1,)
</versionRange>
<goals>
<goal>add-source</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnConfiguration>true</runOnConfiguration>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/annotations/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>