OpenJPA Maven Plugin

As of this writing the OpenJPA Plugin provides 3 goals to cope with persistence-enabled classes in a project using Maven 2.

Goals Overview

  • openjpa:enhance enhances the persistence-enabled classes in a project.
  • openjpa:test-enhance enhances the persistence-enabled test classes in a project. This is typically bound to the process-test-classes phase.
  • openjpa:sql creates a file which contains the SQL statements for creating or updating the database or directly create the schema in the database.
  • openjpa:schema create the schema mapping XML file

    All these OpenJPA Mojos expect the following resources to be present on classpath:

  • META-INF/persistence.xml, or
  • META-INF/openjpa.xml

    OpenJPA documentation is available here.

Examples

Below is an OpenJPA plugin configuration example.

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>openjpa-maven-plugin</artifactId>
    <version>1.2</version>
    <configuration>
      <includes>com/myproject/entities/**/*.class</includes>
      <addDefaultConstructor>true</addDefaultConstructor>
      <enforcePropertyRestrictions>true</enforcePropertyRestrictions>

      <!-- Pass additional properties to the Plugin here -->     
      <toolProperties>
        <property>
          <name>directory</name>
          <value>otherdirectoryvalue</value>
        </property>              
      </toolProperties>

    </configuration>
    <executions>
      <execution>
        <id>enhancer</id>
        <phase>process-classes</phase>
        <goals>
          <goal>enhance</goal>
        </goals>
      </execution>
    </executions>

    <dependencies>
      <dependency>
        <groupId>org.apache.openjpa</groupId>
        <artifactId>openjpa</artifactId>
        <version>2.0.1</version>
      </dependency>
    </dependencies>

  </plugin>
  • You have to explicitly specify an OpenJPA dependency in the dependencies section of the plugin! This has been changed to make sure that the correct OpenJPA version is used for compile time enhancement and other tasks.
  • The openjpa:enhance mojo will automatically be called in the process-classes phase.

    From the command prompt/terminal window.

  • Change directory to the project's root directory.
  • Run the following goal to run OpenJPA PCEnhancer on persistence-enabled classes manually.
      mvn openjpa:enhance
  • Run the following goal to run OpenJPA MappingTool for creating the database creation SQL statements for all persistence-enabled classes manually.
      mvn openjpa:sql
  • Run the following goal to run OpenJPA MappingTool for creating the schema mapping XML file for all persistence-enabled classes manually.
      mvn openjpa:schema