Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

hibernate - Generate the JPA metamodel files using maven-processor-plugin - What is a convenient way for re-generation?

I am trying to use maven-processor-plugin for generating JPA metamodel java files and I set up my pom.xml as 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) to the same packages of their corresponding entities (Entity.java). Hence, I set up outputDirectory in the plugin as

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

The first time of running is ok, but from later times when performing the metamodel java files re-generation, the plugin always turns out an error about file duplication.

My Question is - Is there any way to config the plugin so that it could overwrite the existing files during re-generation?

In fact, to work around

  1. I must delete all generated files before any re-generation.
  2. I could point the outputDirectory to a different folder in /target, this location will be clean everytime Maven run, but this leads to manually copy generated metamodel files to source folder for update after re-generation.

Both of these are very inconvenient and I hope you guys could show me a proper solution.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

March 2018 Answer with Hibernate 5

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

Simply add this to <project> <dependencies> ... </dependencies> </project> in 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 that should be necessary for you to do. If you run into issues, visit the jboss page at the top of this answer.

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

This is not intended to be an original answer, but should prove helpful to anyone who wants a simple, straightforward copy-pastable solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...