【问题标题】:Generate the JPA metamodel files using maven-processor-plugin - What is a convenient way for re-generation?使用 maven-processor-plugin 生成 JPA 元模型文件 - 重新生成的便捷方法是什么?
【发布时间】:2015-08-26 04:09:24
【问题描述】:

我正在尝试使用 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 时,此位置都是干净的,但是这 导致手动将生成的元模型文件复制到源文件夹 重新生成后更新。

这两个都非常不方便,希望大家能给我一个合适的解决方案。

【问题讨论】:

    标签: hibernate maven jpa


    【解决方案1】:

    2018 年 3 月使用 Hibernate 5 回答

    https://docs.jboss.org/hibernate/orm/5.0/topical/html/metamodelgen/MetamodelGenerator.html所述:

    只需将其添加到您的pom.xml 中的&lt;project&gt; &lt;dependencies&gt; ... &lt;/dependencies&gt; &lt;/project&gt;

    <!-- 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 页面。

    此 sn-p 中包含的版本是截至 2018 年 3 月的最新版本,但请查看工件页面 (https://mvnrepository.com/artifact/org.hibernate/hibernate-jpamodelgen) 以获取最新版本。

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

    【讨论】:

    • 没有工作。我必须这样做 + 添加 walkeros 所说的内容。
    【解决方案2】:

    正确的解决方案是生成的源文件应位于目标文件夹中,不应放入源文件夹或您的 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>
    

    【讨论】:

    • 感谢马丁的解释!!!通过使用 build-helper-maven-plugin 并安装一系列相应的 eclipse 插件,现在我的 IDE 可以将外部生成的文件解析为 Java 类,可以从我的代码中的任何位置引用
    • 根据this post,build-helper-meven-plugin 太旧无法使用。有更新的方法吗?我运行 Maven 3.3.3
    • 这是一个很好的解决方案...具体实现~ Intellij 支持添加annotation processors
    【解决方案3】:

    如果您在编译期间使用了另一个注释处理器(如 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>
    

    【讨论】:

    • 我没有错误,但这成功了,谢谢。
    【解决方案4】:

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

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

    特点:

    • JPA 2.1
    • Hibernate 元模型生成器示例
    • EclipseLink 元模型生成器示例
    • 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 2013-06-27
      • 1970-01-01
      • 2015-06-19
      相关资源
      最近更新 更多