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.apache.openjpa</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>

  </plugin>
  • You don't have to explicitly specify an OpenJPA dependency in the dependencies section of the plugin! Follow the following guide to use this plugin with older OpenJPA versions.
  • 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