使用maven-processor-plugin生成JPA元模型文件 - 重新生成的便捷方式是什么?

11

我正在尝试使用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文件时,插件总是出现文件重复的错误。

我的问题是: -有没有办法配置插件,使其在重新生成过程中覆盖现有文件?

事实上,为了解决这个问题,我必须采取以下措施:

  1. 在重新生成之前,我必须删除所有已生成的文件。
  2. 我可以将outputDirectory指向/target中的不同文件夹,每次Maven运行都会清除该位置,但这 需要在重新生成之后手动将生成的元模型文件复制到源文件夹进行更新。

这两种方法都非常不方便,我希望你们能给我一个合适的解决方案。

4个回答

13

使用Hibernate 5的2018年3月答案

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)以获取最新版本。

这并不是原创答案,但应该对任何想要简单、直接复制粘贴的解决方案有所帮助。


没有起作用。我不得不这样做+添加Walkeros所说的内容。 - Flyout91

10

合适的解决方案是,生成的源代码应该位于目标文件夹中,而不应该放在源文件夹或版本控制系统中。

当然,将生成的源代码放入目标文件夹中会在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>

感谢Martin的解释!!!通过使用build-helper-maven-plugin并安装一系列对应的Eclipse插件,现在我的IDE可以将外部生成的文件解析为Java类,并且可以在代码的任何地方引用它们。 - Chiến Nghê
2
build-helper-maven-plugin太旧了,根据this post,不能使用。是否有更新的方法?我使用的是Maven 3.3.3 - Solver42
这是一个很好的解决方案... 实现特定 ~ Intellij支持添加注释处理器 - Edward J Beckett

8

如果在编译期间使用了其他注解处理器(如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>

我没有错误,但这个方法很有效,谢谢。 - Flyout91

2

我也有一个可行的解决方案,希望与其他人分享。所以我希望这是正确的位置。

代码可以在GitHub找到https://github.com/StefanHeimberg/jpa21-maven-metagen

功能:

  • JPA 2.1
  • 使用Hibernate Metamodel Generator的示例
  • 使用EclipseLink Metamodel Generator的示例
  • IntelliJ(已测试14.1.4)和NetBeans(已测试8.1)的最小Maven配置
  • Eclipse的M2E解决方法(已测试4.5.1)(自动激活配置文件)

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接