Create JPA metamodel files using maven-processor-plugin - What is a convenient way to re-generate?

I am trying to use the maven-processor-plugin to create a JPA metamodel of java files and I have configured my pom.xml like belows.

<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>

      

Actually, I want to generate metamodel files (Entity_.java) into the same packages of their respective entities (Entity.java). Hence, I am setting outputDirectory in the plugin as

<outputDirectory>${basedir}/src/main/java</outputDirectory>

      

The first time it works fine, but from later times, when re-generating the metamodel of java files, the plugin always gives an error about duplicate files.

My question is - Is there a way to configure the plugin so that it can overwrite existing files during re-generation?

In fact, to get around

  • I have to delete all generated files before any re-generation.
  • I can point the outputDirectory to a different folder in / target, this location will be clean every time Maven starts, but this causes the generated meta-model files to be manually copied to the source folder for updating after re-generation.

Both are very inconvenient and I hope you guys can show me the right solution.

+3


source to share


3 answers


The correct solution is that the generated sources should be in the target folder and should not be placed in the source folders or your SCM system.

Of course, by putting your generated sources on target, you would run into a problem in your IDE, because the associated code cannot be found. Therefore, you can add the build-helper-maven plugin to dynamically add a folder from the target directory.



<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>

      

+5


source


I also have a working solution and I want to share this with others. so I hope this is the right place for this.

the code can be found on GitHub https://github.com/StefanHeimberg/jpa21-maven-metagen

functions:



  • JPA 2.1
  • Hibernation metamodel generator example
  • EclipseLink metamodel generator example
  • Minimal Maven configuration for IntelliJ (tested since 14.1.4) and NetBeans (tested since 8.1)
  • M2E Workaround for Eclipse (tested with 4.5.1) (Automatic Profile Activation)

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 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>

      

0


source


March 2018 Answer with Hibernate 5

As described at https://docs.jboss.org/hibernate/orm/5.0/topical/html/metamodelgen/MetamodelGenerator.html :

Just add this <project> <dependencies> ... </dependencies> </project>

to your 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>

      

There is nothing else to do for you. If you run into problems, please go to the jboss page at the top of this answer.

The version included in this snippet is the latest version from March 2018, but check the artifact page ( https://mvnrepository.com/artifact/org.hibernate/hibernate-jpamodelgen ) for the latest version.

This shouldn't be the original answer, but should prove to be helpful for anyone looking for a simple, straight forward copy solution.

0


source







All Articles