Chapter 13.  Third Party Integration

Table of Contents

1. Apache Ant
1.1. Common Ant Configuration Options
1.2. Enhancer Ant Task
1.3. Application Identity Tool Ant Task
1.4. Mapping Tool Ant Task
1.5. Reverse Mapping Tool Ant Task
1.6. Schema Tool Ant Task

OpenJPA provides a number of mechanisms for integrating with third-party tools. The following chapter will illustrate these integration features.

1.  Apache Ant

Ant is a very popular tool for building Java projects. It is similar to the make command, but is Java-centric and has more modern features. Ant is open source, and can be downloaded from Apache's Ant web page at http://jakarta.apache.org/ant/ . Ant has become the de-facto standard build tool for Java, and many commercial integrated development environments provide some support for using ant build files. The remainder of this section assumes familiarity with writing Ant build.xml files.

OpenJPA provides pre-built Ant task definitions for all bundled tools:

The source code for all the ant tasks is provided with the distribution under the src directory. This allows you to customize various aspects of the ant tasks in order to better integrate into your development environment.

1.1.  Common Ant Configuration Options

All OpenJPA tasks accept a nested config element, which defines the configuration environment in which the specified task will run. The attributes for the config tag are defined by the JDBCConfiguration bean methods. Note that excluding the config element will cause the Ant task to use the default system configuration mechanism, such as the configuration defined in the org.apache.openjpa.xml file.

Following is an example of how to use the nested config tag in a build.xml file:

Example 13.1.  Using the <config> Ant Tag

<mappingtool>
  <fileset dir="${basedir}">
    <include name="**/model/*.java" />
  </fileset>
  <config connectionUserName="scott" connectionPassword="tiger"
    connectionURL="jdbc:oracle:thin:@saturn:1521:solarsid"
    connectionDriverName="oracle.jdbc.driver.OracleDriver" />
</mappingtool>

It is also possible to specify a properties or propertiesFile attribute on the config tag, which will be used to locate a properties resource or file. The resource will be loaded relative to the current CLASSPATH.

Example 13.2.  Using the Properties Attribute of the <config> Tag

<mappingtool>
  <fileset dir="${basedir}">
    <include name="**/model/*.java"/>
  </fileset>
  <config properties="openjpa-dev.xml"/>
</mappingtool>

Example 13.3.  Using the PropertiesFile Attribute of the <config> Tag

<mappingtool>
  <fileset dir="${basedir}">
    <include name="**/model/*.java"/>
  </fileset>
  <config propertiesFile="../conf/openjpa-dev.xml"/>
</mappingtool>

Tasks also accept a nested classpath element, which you can use in place of the default classpath. The classpath argument behaves the same as it does for Ant's standard javac element. It is sometimes the case that projects are compiled to a separate directory than the source tree. If the target path for compiled classes is not included in the project's classpath, then a classpath element that includes the target class directory needs to be included so the enhancer and mapping tool can locate the relevant classes.

Following is an example of using a classpath tag:

Example 13.4.  Using the <classpath> Ant Tag

<openjpac>
  <fileset dir="${basedir}/source">
    <include name="**/model/*.java" />
  </fileset>
  <classpath>
    <pathelement location="${basedir}/classes"/>
    <pathelement location="${basedir}/source"/>
    <pathelement path="${java.class.path}"/>
  </classpath>
</openjpac>

Finally, tasks that invoke code-generation tools like the application identity tool and reverse mapping tool accept a nested codeformat element. See the code formatting documentation in Section 3.1, “ Code Formatting ” for a list of code formatting attributes.

Example 13.5.  Using the <codeformat> Ant Tag

<reversemappingtool package="com.xyz.jdo" directory="${basedir}/src">
  <codeformat tabSpaces="4" spaceBeforeParen="true" braceOnSameLine="false"/>
</reversemappingtool>

1.2.  Enhancer Ant Task

The enhancer task allows you to invoke the OpenJPA enhancer directly from within the Ant build process. The task's parameters correspond exactly to the long versions of the command-line arguments to the org.apache.openjpa.enhance.PCEnhancer.

The enhancer task accepts a nested fileset tag to specify the files that should be processed. You can specify .java or .class files. If you do not specify any files, the task will run on the classes listed in your persistence.xml or openjpa.MetaDataFactory property.

Following is an example of using the enhancer task in a build.xml file:

Example 13.6.  Invoking the Enhancer from Ant

<target name="enhance">
  <!-- define the openjpac task; this can be done at the top of the    -->
  <!-- build.xml file, so it will be available for all targets      -->
  <taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask"/>

  <!-- invoke enhancer on all .java files below the model directory -->
  <openjpac>
    <fileset dir=".">
      <include name="**/model/*.java" />
    </fileset>
  </openjpac>
</target>

1.3.  Application Identity Tool Ant Task

The application identity tool task allows you to invoke the application identity tool directly from within the Ant build process. The task's parameters correspond exactly to the long versions of the command-line arguments to the org.apache.openjpa.enhance.ApplicationIdTool.

The application identity tool task accepts a nested fileset tag to specify the files that should be processed. You can specify .java or .class files. If you do not specify any files, the task will run on the classes listed in your persistence.xml file or openjpa.MetaDataFactory property.

Following is an example of using the application identity tool task in a build.xml file:

Example 13.7.  Invoking the Application Identity Tool from Ant

<target name="appids">
  <!-- define the appidtool task; this can be done at the top of     -->
  <!-- the build.xml file, so it will be available for all targets   -->
  <taskdef name="appidtool" classname="org.apache.openjpa.ant.ApplicationIdToolTask"/>

  <!-- invoke tool on all .jdo files below the current directory     -->
  <appidtool>
    <fileset dir=".">
      <include name="**/model/*.java" />
    </fileset>
    <codeformat spaceBeforeParen="true" braceOnSameLine="false"/>
  </appidtool>
</target>

1.4.  Mapping Tool Ant Task

The mapping tool task allows you to directly invoke the mapping tool from within the Ant build process. It is useful for making sure that the database schema and object-relational mapping data is always synchronized with your persistent class definitions, without needing to remember to invoke the mapping tool manually. The task's parameters correspond exactly to the long versions of the command-line arguments to the org.apache.openjpa.jdbc.meta.MappingTool.

The mapping tool task accepts a nested fileset tag to specify the files that should be processed. You can specify .java or .class files. If you do not specify any files, the task will run on the classes listed in your persistence.xml file or openjpa.MetaDataFactory property.

Following is an example of a build.xml target that invokes the mapping tool:

Example 13.8.  Invoking the Mapping Tool from Ant

<target name="refresh">
  <!-- define the mappingtool task; this can be done at the top of -->
  <!-- the build.xml file, so it will be available for all targets -->
  <taskdef name="mappingtool" classname="org.apache.openjpa.jdbc.ant.MappingToolTask"/>

  <!-- add the schema components for all .jdo files below the      -->
  <!-- current directory                                           -->
  <mappingtool action="buildSchema">
    <fileset dir=".">
      <include name="**/*.jdo" />
    </fileset>
  </mappingtool>
</target>

1.5.  Reverse Mapping Tool Ant Task

The reverse mapping tool task allows you to directly invoke the reverse mapping tool from within Ant. While many users will only run the reverse mapping process once, others will make it part of their build process. The task's parameters correspond exactly to the long versions of the command-line arguments to the org.apache.openjpa.jdbc.meta.ReverseMappingTool.

Following is an example of a build.xml target that invokes the reverse mapping tool:

Example 13.9.  Invoking the Reverse Mapping Tool from Ant

<target name="reversemap">
  <!-- define the reversemappingtool task; this can be done at the top of -->
  <!-- the build.xml file, so it will be available for all targets        -->
  <taskdef name="reversemappingtool" 
    classname="org.apache.openjpa.jdbc.ant.ReverseMappingToolTask"/>

  <!-- reverse map the entire database -->
  <reversemappingtool package="com.xyz.model" directory="${basedir}/src"
    customizerProperties="${basedir}/conf/reverse.properties">
    <codeformat tabSpaces="4" spaceBeforeParen="true" braceOnSameLine="false"/>
  </reversemappingtool>
</target>

1.6.  Schema Tool Ant Task

The schema tool task allows you to directly invoke the schema tool from within the Ant build process. The task's parameters correspond exactly to the long versions of the command-line arguments to the org.apache.openjpa.jdbc.schema.SchemaTool.

Following is an example of a build.xml target that invokes the schema tool:

Example 13.10.  Invoking the Schema Tool from Ant

<target name="schema">
  <!-- define the schematool task; this can be done at the top of  -->
  <!-- the build.xml file, so it will be available for all targets -->
  <taskdef name="schematool" classname="org.apache.openjpa.jdbc.ant.SchemaToolTask"/>

  <!-- add the schema components for all .schema files below the   -->
  <!-- current directory                                           -->
  <schematool action="add">
    <fileset dir=".">
      <include name="**/*.schema" />
    </fileset>
  </schematool>
</target>