Hibernate 使用maven处理器插件生成JPA元模型文件-什么是方便的重新生成方法?

Hibernate 使用maven处理器插件生成JPA元模型文件-什么是方便的重新生成方法?,hibernate,maven,jpa,Hibernate,Maven,Jpa,我正在尝试使用maven处理器插件生成JPA元模型java文件,我将pom.xml设置如下 <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target>

我正在尝试使用maven处理器插件生成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>

maven编译器插件
1.8
1.8
-过程:无
org.bsc.maven
maven处理器插件
过程
过程
生成源
${basedir}/src/main/java
org.hibernate.jpamodelgen.jpametamodelenticyprocessor
真的
实际上,我想将元模型文件(Entity.java)生成到相应实体(Entity.java)的相同包中。因此,我在插件中设置outputDirectory为

<outputDirectory>${basedir}/src/main/java</outputDirectory>
${basedir}/src/main/java
第一次运行是可以的,但是在以后执行元模型java文件重新生成时,插件总是会出现文件复制错误

我的问题是 -有没有办法配置插件,以便在重新生成时覆盖现有文件

事实上,我们需要努力

  • 在任何重新生成之前,我必须删除所有生成的文件
  • 我可以将outputDirectory指向/target中的另一个文件夹,每次运行Maven时,这个位置都是干净的,但是 导致手动将生成的元模型文件复制到源文件夹 用于重新生成后的更新

  • 这两个都很不方便,我希望你们能给我一个合适的解决方案。

    合适的解决方案是生成的源应该在目标文件夹中,不应该放在源文件夹或SCM系统中

    当然,通过将生成的源代码放入目标中,您将面临IDE中的问题,因为找不到相关代码。因此,您可以添加buildhelpermaven插件,从目标目录动态添加文件夹

    <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>
    
    
    org.bsc.maven
    maven处理器插件
    过程
    过程
    生成源
    ${project.build.directory}/generated sources/java/jpametamodel
    org.hibernate.jpamodelgen.jpametamodelenticyprocessor
    真的
    org.codehaus.mojo
    构建助手maven插件
    添加源
    生成源
    添加源
    ${project.build.directory}/generated sources/java/jpametamodel
    
    我还有一个可行的解决方案,我想与其他人分享。所以我希望这是正确的地方

    代码可以在GitHub上找到

    功能:

    <?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>
    
    • 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>
    
    
    4.0.0
    com.github.stefanheimberg
    jpa21 maven metagen
    1.0-快照
    罐子
    UTF-8
    1.8
    1.8
    爪哇
    JavaEEAPI
    7
    假如
    冬眠
    真的
    5.0.3.4最终版本
    org.hibernate
    冬眠
    ${hibernate.version}
    假如
    日食
    假的
    2.6.1
    org.eclipse.persistence
    org.eclipse.persistence.jpa.modelgen.processor
    ${eclipselink.version}
    假如
    m2e
    m2e.version
    org.eclipse.m2e
    生命周期映射
    1.0.0
    org.codehaus.mojo
    构建助手maven插件
    [1.9.1,)
    添加源
    真的
    真的
    
    [INFO] -------------------------------------------------------------
    [ERROR] COMPILATION ERROR :
    [INFO] -------------------------------------------------------------
    [ERROR] /your/not/CompilableClass:[5,68] cannot find symbol
      symbol:   class YouEntity_
      location: package your.not
    [INFO] 1 error
    
                <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>