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
Both of these are very inconvenient and I hope you guys could show me a proper solution.
Generating JPA Metamodel Classes Note, we need to add the target/generated-classes folder to the classpath of our IDE, as by default, the classes will be generated in this folder only.
JPA (Java persistence API) metamodel classes are classes that describe the structure of JPA entity classes (classes used to store database state as Java objects). They enable you to write Java code for creating database queries in a typesafe way.
The Metamodel API is used to create a metamodel of the managed entities in a particular persistence unit. For each entity class in a particular package, a metamodel class is created with a trailing underscore and with attributes that correspond to the persistent fields or properties of the entity class.
A maven plugin to process annotation for jdk6 at compile time This plugin helps to use from maven the new annotation processing provided by JDK6 integrated in java compiler This plugin could be considered the 'alter ego' of maven apt plugin http://mojo.codehaus.org/apt-maven-plugin/
In case you your have another annotation processor used during compilation (like lombok) you might run into following error (depite having hibernate-jpamodelgen added as dependency):
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /your/not/CompilableClass:[5,68] cannot find symbol
symbol: class YouEntity_
location: package your.not
[INFO] 1 error
In this case you need to declare both annotation processors directly for your compiler plugin (for both lombok and 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With