It is often better to enhance your entities at build times and it generally simply needs to add -Aopenjpa.metamodel=true to maven compiler compilerArguments and openjpa to the classpath of the plugin but it has two main drawbacks:
- if you need to use other processors you can’t manage it correctly
- it simply doesn’t work with maven compiler plugin 3.0
Of course there is an interesting alternative.
It simply consists to use two other plugins:
- maven-processor-plugin to configure the generation (processor)
- build-helper-maven-plugin to attach generated sources
For the impatient here is the snippet:
<plugin> <groupId>org.bsc.maven</groupId> <artifactId>maven-processor-plugin</artifactId> <version>2.1.0</version> <executions> <execution> <id>process</id> <goals> <goal>process</goal> </goals> <phase>generate-sources</phase> <configuration> <compilerArguments>-Aopenjpa.source=7 -Aopenjpa.metamodel=true</compilerArguments> <processors> <processor>org.apache.openjpa.persistence.meta.AnnotationProcessor6</processor> </processors> <outputDirectory>target/generated-sources/metamodel</outputDirectory> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa</artifactId> <version>${openjpa.version}</version> </dependency> </dependencies> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.7</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>target/generated-sources/metamodel</source> </sources> </configuration> </execution> </executions> </plugin>
The first plugin simply generates the metamodel. The option openjpa.source=7 simply makes it working smoothly with java 7 (it considers java 6 by default). Listing processors is here optional since javac is able to find them from the classpath (but allows you to specify explicitely which one to use). We generate the metamodel in target/generated-sources/metamodel and then with the second plugin we simply add it as sources too to let them be compiled.
Note: it can be interesting to add to maven-compiler-plugin the argument -proc:none if you want to totally control the processor usage.
Interesting: what’s the difference with [1], i.e. the openjpa-maven-plugin?
[1] http://openjpa.apache.org/enhancement-with-maven.html
right, my title was wrong (corrected normally), openjpa-maven-plugin doesn’t support metamodel generation actually
Note it works 🙂
see workaround in https://jira.codehaus.org/browse/MCOMPILER-197