DdlUtils comes with two Ant tasks that allow you to manipulate the database structure, insert data into the database, dump the database structure and data contained in it, to XML, etc.
Lets see examples for how to use them:
<path id="runtime-classpath"> <fileset dir="lib"> <include name="**/*.jar"/> <include name="**/*.zip"/> </fileset> </path> <target name="database-setup" description="Creates the database structure and inserts data into the database"> <taskdef name="ddlToDatabase" classname="org.apache.ddlutils.task.DdlToDatabaseTask"> <classpath refid="runtime-classpath"/> </taskdef> <ddlToDatabase> <database url="jdbc:postgresql://localhost/test" driverClassName="org.postgresql.Driver" username="someuser" password="somepassword"/> <fileset dir="src/schema"> <include name="project-schema.xml"/> </fileset> <createDatabase failonerror="false"/> <writeSchemaToDatabase/> <writeDataToDatabase datafile="src/data/data.xml"/> </ddlToDatabase> </target>
This snippet essentially uses the DdlToDatabaseTask
task to create a PostgreSQL
database at //localhost/test
, establish the database structure (tables, foreign keys
etc.) defined in the file src/schema/project-schema.xml
in this new database,
and then insert the data contained in src/data/data.xml
.
In order for this to work, both DdlUtils and the JDBC driver have to be available
in the path specified by runtime-classpath
. In the above snippet, this path
contains all JARs and ZIPs in sub-directory lib
.
The opposite direction is achieved via the DatabaseToDdlTask
task:
<path id="runtime-classpath"> <fileset dir="lib"> <include name="**/*.jar"/> <include name="**/*.zip"/> </fileset> </path> <target name="database-dump" description="Dumps the database structure"> <taskdef name="databaseToDdl" classname="org.apache.ddlutils.task.DatabaseToDdlTask"> <classpath refid="runtime-classpath"/> </taskdef> <databaseToDdl modelName="MyModel"> <database url="jdbc:derby:ddlutils" driverClassName="org.apache.derby.jdbc.EmbeddedDriver" username="" password=""/> <writeSchemaToFile outputFile="db-schema.xml"/> <writeDataToFile outputFile="data.xml"/> </databaseToDdl> </target>
Here, the database schema is retrieved using the specified JDBC driver, and then written
to the file db-schema.xml
. Likewise, the data in the database is written
to the file data.xml
.
The DdlUtils tasks require Ant version 1.5 or newer.
The reference documentation for the Ant tasks can be found here.
©2005-2007 Apache Software Foundation