February 9th, 2012

Automatically generate your JPA Metamodel classes using OpenJPA and Maven

A simple pom.xml for automating the generation of JPA Metamodel classes for OpenJPA using Maven

— Matthew Fellows —

I found several potential candidate posts on the Internet that I thought would help me with this simple problem. As it turns out, it is simple, but get a few params slightly wrong and you’re in a mess.

Here is the plugin section of my pom.xml, it will generate JPA Metamodel classes into the target/generated-sources directory of your build and compile them into the end product.

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>

                    <executions>
                        <execution>
                            <id>generate-entity-metamodel</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                            <configuration>
                                <source>1.6</source>
                                <target>1.6</target>
                                <optimize>true</optimize>
                                <showDeprecation>true</showDeprecation>
                                <showWarnings>true</showWarnings>
                                <proc>only</proc>
                                <compilerArgument>-Aopenjpa.metamodel=true</compilerArgument>
                                <generatedSourcesDirectory>${project.build.directory}/generated-sources</generatedSourcesDirectory>
                            </configuration>
                        </execution>
                        <execution>
                            <id>compile</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                            <configuration>
                                <source>1.6</source>
                                <target>1.6</target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

If you want to store these classes in your SCM, try the following instead:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>

                    <executions>
                        <execution>
                            <id>generate-entity-metamodel</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>
                                    compile
                                </goal>
                            </goals>
                            <configuration>
                                <source>1.6</source>
                                <target>1.6</target>
                                <optimize>true</optimize>
                                <showDeprecation>true</showDeprecation>
                                <showWarnings>true</showWarnings>
                                <proc>only</proc>
                                <compilerArgument>-Aopenjpa.metamodel=true</compilerArgument>
                                <generatedSourcesDirectory>${basedir}/src/main/java</generatedSourcesDirectory>
                            </configuration>
                        </execution>
                        <execution>
                            <id>compile</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>
                                    compile
                                </goal>
                            </goals>
                            <configuration>
                                <source>1.6</source>
                                <target>1.6</target>
                                <generatedSourcesDirectory>${basedir}/src/main/java</generatedSourcesDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

Hopefully this helps somebody out there.