You might gain some reusability and better Ant code by using macrodef.
http://ant.apache.org/manual/CoreTasks/macrodef.html

Here is the macrodef I'm using for example :

    <macrodef name="enhance"
description="This will enhance annotated Java entity classes.">

        <attribute name="class-dir"
description="The class folder where to find .class file annotated and enhanced using OpenJPA"/> <attribute name="persistence-xml-file" description="The full path to the persistence.xml file"/>
        <attribute name="lib-dir"
description="Folder where to find /openjpa and / jpa folders that contains all the .jar"/>

        <sequential>
<taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask">
                <classpath>
                    <fileset dir="@{lib-dir}/openjpa">
                        <include name="**/*.jar"/>
                    </fileset>
                    <fileset dir="@{lib-dir}/jpa">
                        <include name="**/*.jar"/>
                    </fileset>
                    <pathelement location="@{class-dir}"/>
                </classpath>
            </taskdef>
            <openjpac>
                <config propertiesFile="@{persistence-xml-file}"/>
                <classpath>
                    <fileset dir="@{lib-dir}/openjpa">
                        <include name="**/*.jar"/>
                    </fileset>
                    <fileset dir="@{lib-dir}/jpa">
                        <include name="**/*.jar"/>
                    </fileset>
                    <pathelement location="@{class-dir}"/>
                </classpath>
            </openjpac>
            <mkdir dir="@{class-dir}/META-INF"/>
<copy todir="@{class-dir}/META-INF" description="Copy persistence.xml to META-INF" overwrite="yes">
                <fileset file="@{persistence-xml-file}"/>
            </copy>
        </sequential>
    </macrodef>

    <macrodef name="install-schema"
description="This will install or upgrade the database schema from annotated and enhanced Java classes.">

        <attribute name="class-dir"
description="The class folder where to find .class file annotated and enhanced using OpenJPA"/> <attribute name="persistence-xml-file" description="The full path to the persistence.xml file"/>
        <attribute name="lib-dir"
description="Folder where to find /openjpa and / jpa folders that contains all the .jar"/> <attribute name="jdbc-lib-dir" description="Full path to folder that contains JDBC drivers jar"/>

        <sequential>
<taskdef name="mappingtool" classname="org.apache.openjpa.jdbc.ant.MappingToolTask">
                <classpath>
                    <fileset dir="@{lib-dir}/openjpa">
                        <include name="**/*.jar"/>
                    </fileset>
                    <fileset dir="@{lib-dir}/jpa">
                        <include name="**/*.jar"/>
                    </fileset>
                    <pathelement location="@{class-dir}"/>
                </classpath>
            </taskdef>

            <mappingtool action="buildSchema">
                <config propertiesFile="@{persistence-xml-file}"/>

                <classpath>
                    <fileset dir="@{lib-dir}/openjpa">
                        <include name="**/*.jar"/>
                    </fileset>
                    <fileset dir="@{lib-dir}/jpa">
                        <include name="**/*.jar"/>
                    </fileset>
                    <fileset dir="@{jdbc-lib-dir}">
                        <include name="**/*.jar"/>
                    </fileset>
                    <pathelement location="@{class-dir}"/>
                </classpath>
            </mappingtool>
        </sequential>
    </macrodef>


On Sep 28, 2009, at 20:49 , Marc.Boudreau wrote:


I had been struggling with this feature for a few days and finally figured it out. Since none of the posts that I found in my many searches led me to the resolution, I thought I would post what I was doing wrong and what I did to
fix it.

Our project is required to generate the SQL scripts to create the schema for our 6 supported database vendors at build time. So I edited the ANT script
used to build the project and added the following target:
<target name="jpa_generate_sqlscripts">
        <mkdir dir="schema/${vendor}"/>
        <taskdef name="mappingtool"
classname="org.apache.openjpa.jdbc.ant.MappingToolTask"
classpathref="classpath.openjpa"/>
        
        <mappingtool action="buildSchema"
sqlFile="${basedir}/schema/${vendor}/create.sql">
                <config DBDictionary="${vendor}"
propertiesFile="META-INF/persistence.xml"/>
                <classpath>
                        <path refid="classpath.openjpa"/>
                        <pathelement location="bin"/>
                </classpath>
        </mappingtool>
</target>

I then added antcalls to my new target like this:
<antcall target="jpa_generate_sqlscripts">
        
</antcall>
... and so forth for the 5 other vendors.

After careful scrutiny of the documentation of the options/flags for the
Mapping Tool
(http://openjpa.apache.org/builds/1.2.1/apache-openjpa-1.2.1/docs/manual/manual.html#ref_guide_mapping_mappingtool ), I realized that "buildSchema" is the default value for the action attribute, so I omitted it. And then looking at the documentation of the options for
the Schema Tool
(http://openjpa.apache.org/builds/1.2.1/apache-openjpa-1.2.1/docs/manual/manual.html#ref_guide_schema_schematool ) I saw that "add" is the default action, but what I need is "build", so I
added the schemaAction attribute and set its value to "build".

At this point I thought I had it for sure. But, the tool still tried to
connect to the database but failed because I didn't specify a
ConnectionDriverName (or any other connection details for that matter).

Finally, I saw that there is an argument named "readSchema". This is a boolean flag and the documentation doesn't say which is the default value. But after I set this option to false in my ANT script, the tool succeeded.
This is the final ANT target:
<target name="jpa_generate_sqlscripts">
        <mkdir dir="schema/${vendor}"/>
        <taskdef name="mappingtool"
classname="org.apache.openjpa.jdbc.ant.MappingToolTask"
classpathref="classpath.openjpa"/>
        
        <mappingtool schemaAction="build"
sqlFile="${basedir}/schema/${vendor}/create.sql" readSchema="false">
                <config DBDictionary="${vendor}"
propertiesFile="META-INF/persistence.xml"/>
                <classpath>
                        <path refid="classpath.openjpa"/>
                        <pathelement location="bin"/>
                </classpath>
        </mappingtool>
</target>


Marc Boudreau
--
View this message in context: 
http://n2.nabble.com/Generating-DDL-without-connecting-to-Database-OpenJPA-1-2-1-tp3731174p3731174.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Reply via email to