Generate OpenJPA metamodel classes with maven, maven compiler alternative


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:

  1. if you need to use other processors you can’t manage it correctly
  2. 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:

  1. maven-processor-plugin to configure the generation (processor)
  2. 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.

Advertisement

3 thoughts on “Generate OpenJPA metamodel classes with maven, maven compiler alternative

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s