Chi*_*ghê 7 hibernate jpa maven
我正在尝试使用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>
Run Code Online (Sandbox Code Playgroud)
实际上,我想生成元模型文件(Entity_.java)到相应实体(Entity.java)的相同包中.因此,我在插件中设置outputDirectory为
<outputDirectory>${basedir}/src/main/java</outputDirectory>
Run Code Online (Sandbox Code Playgroud)
第一次运行是好的,但是从以后执行元模型java文件重新生成时,插件总是会出现关于文件复制的错误.
我的问题是 - 有没有办法配置插件,以便它可以在重新生成过程中覆盖现有的文件?
事实上,要解决
这两个都非常不方便,我希望你们能给我一个合适的解决方案.
正确的解决方案是生成的源应该位于目标文件夹中,不应该放入源文件夹或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>
Run Code Online (Sandbox Code Playgroud)
如果您在编译期间使用了另一个注释处理器(例如 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
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您需要直接为编译器插件声明两个注释处理器(对于 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>
Run Code Online (Sandbox Code Playgroud)
如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>
Run Code Online (Sandbox Code Playgroud)
你不需要做任何其他事情.如果您遇到问题,请访问此答案顶部的jboss页面.
此代码段中包含的版本是截至2018年3月的最新版本,但请查看工件页面(https://mvnrepository.com/artifact/org.hibernate/hibernate-jpamodelgen)以获取最新版本.
这不是一个原始的答案,但应该对任何想要一个简单,直接的可复制的解决方案的人有所帮助.
归档时间: |
|
查看次数: |
8679 次 |
最近记录: |