The following section outlines the necessary steps to configure a Torque-based ORM project using ant. For this, you need to create ant's build.xml file and make the necessary libraries available. It is recommended to run the tutorial against a mysql database, as all of the explanation in this tutorial assumes that you use mysql.
As a starting point, create a directory as a base directory for your project (also called the project's top level directory). All the paths in the following steps will be relative to this base directory.
As a starting point for the build file in your project, use the following template and save it as build.xml in the project's base directory. Then edit it to reflect your specific needs (typically you need to change the database URLs, the database host, the database user and password):
<?xml version="1.0"?> <project name="Torque" default="main" basedir="."> <path id="ant-classpath"> <fileset dir="lib/ant"> <include name="*.jar"/> </fileset> </path> <path id="runtime-classpath"> <fileset dir="lib/runtime"> <include name="*.jar"/> </fileset> </path> <taskdef name="torque-generator" classpathref="ant-classpath" classname="org.apache.torque.ant.task.TorqueGeneratorTask"/> <target name="generate" description="==> generates sql + om classes"> <torque-generator packaging="classpath" configPackage="org.apache.torque.templates.om" sourceDir="src/main/schema"> <option key="torque.om.package" value="org.apache.torque.tutorial.om"/> <option key="torque.database" value="mysql"/> </torque-generator> <torque-generator packaging="classpath" configPackage="org.apache.torque.templates.sql" sourceDir="src/main/schema" defaultOutputDir="target/generated-sql"> <option key="torque.database" value="mysql"/> </torque-generator> </target> <target name="compile"> <mkdir dir="target/classes"/> <javac debug="on" source="1.5" destdir="target/classes"> <src path="src/main/java"/> <src path="src/main/generated-java"/> <src path="target/generated-sources"/> <classpath refid="runtime-classpath"/> </javac> <copy todir="target/classes"> <fileset dir="src/main/resources"/> </copy> </target> <target name="execute-sql"> <sql classpathref="ant-classpath" driver="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost:3306/bookstore" userid="root" password="password" onerror="continue" src="target/generated-sql/bookstore-schema.sql"/> </target> <target name="clean"> <delete dir="target" /> </target> <target name="main" description="build all" depends="generate, compile"> </target> </project>
This build file contains the following definitions:
A correct ant build file is very important. This enables the Torque generator to generate all of the required sources and SQL for your specific database. If you experience problems later in this tutorial, it would be wise to double-check this file.
For the Torque generator and SQL ant tasks to work correctly, you need to provide them with some additional libraries. This is done as follows:
This completes the configuration of the Torque ant tasks (and other settings to be made in the build.xml).
Next we will look at Defining the database schema.