Reality-Loop

How to export your database schema with Hibernate3 and Ant

August 28, 2008 | 0 Minute Read
This post was originally posted on my old blog.

There is a lot of old information floating around on the net. Currently the correct way to export your database schema with Hibernate3 and Ant is using Hibernate-Tools.

The according Ant-task is org.hibernate.tool.ant.HibernateToolTask (documentation is here). It is contained in hibernate-tools.jar

The resulting section in build.xml should look like this:

	
<!-- Hibernate Tools import -->
<taskdef name="hibernatetool"
		 classname="org.hibernate.tool.ant.HibernateToolTask"
		 classpathref="lib.classpath"/>

<!-- Export the database schema -->
<target name="schemaexport" description="Exports a generated schema to DB and file">

	<hibernatetool destdir="${basedir}">
		<classpath refid="build.classpath"/>
		<!-- Use META-INF/persistence.xml 
			<jpaconfiguration/>-->
		<annotationconfiguration configurationfile="${build.dir}/hibernate.cfg.xml" />
		<hbm2ddl
				drop="true"
				create="true"
				export="true"
				outputfilename="ddl.sql"
				delimiter=";"
				format="true"/>
	</hibernatetool>
</target>

Unfortunately most of the documantation of Hibernate-Tools seems to imply that it is "just" an eclipse-plugin. The truth is, the ant-tasks are completely usable on their own.

The Ant-task org.hibernate.tool.hbm2ddl.SchemaExportTask seems to be depreciated. Even though it is still contained in hibernate3.jar...

See also this page.