<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vonk's Blog &#187; sql</title>
	<atom:link href="http://blog.larsvonkconsultancy.nl/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.larsvonkconsultancy.nl</link>
	<description>Blogs about Software Development, Agility and all the Rest</description>
	<lastBuildDate>Fri, 31 Jul 2009 13:51:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generating sql files in Maven using hibernate tools</title>
		<link>http://blog.larsvonkconsultancy.nl/2008/12/generating-sql-files-in-maven-using-hibernate-tools/</link>
		<comments>http://blog.larsvonkconsultancy.nl/2008/12/generating-sql-files-in-maven-using-hibernate-tools/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 21:42:01 +0000</pubDate>
		<dc:creator>Lars Vonk</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[hbm2ddl]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://lvonk.wordpress.com/?p=27</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Hibernate&#8217;s SessionFactory has some very powerful functions available like generating your tables from your hbm files using the <a href="http://www.hibernate.org/hib_docs/v3/reference/en/html/configuration-optional.html" onclick="pageTracker._trackPageview('/outgoing/www.hibernate.org/hib_docs/v3/reference/en/html/configuration-optional.html?referer=');">hbm2ddl.auto</a> property. This is very handy for laptop testing where you can let hibernate generate your complete scheme on the fly.<br />
Although useful on your laptop, it is probably not want you want to do in a real environment. Luckily <a href="http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html_single/" onclick="pageTracker._trackPageview('/outgoing/docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html_single/?referer=');">hibernate tools</a> provides generating sql files via an Ant task. But if you are using Maven like me how can we integrate this in our build?</p>
<p><span id="more-27"></span></p>
<p>The answer is the <a href="http://maven.apache.org/plugins/maven-antrun-plugin/" onclick="pageTracker._trackPageview('/outgoing/maven.apache.org/plugins/maven-antrun-plugin/?referer=');">maven-antrun-plugin</a>. This plugin allows you to integrate ant tasks in Maven builds. To get the sql files we first create the Ant build.xml that will call the Ant task provided in the hibernate tools</p>
<pre class="brush: xml;">[/xml]
<project name="spark">
	<taskdef classname="org.hibernate.tool.ant.HibernateToolTask" name="hibernatetool" classpath="${test_classpath}" />

	<target name="generate-sql">

		<delete dir="${output_dir}" quiet="true"/>
		<mkdir dir="${output_dir}"/>
		<hibernatetool destdir="${output_dir}">

			<configuration propertyfile="${hibernate_testdir}/hibernate-for-sql-generation.properties">
			    <fileset dir="${hibernate_dir}">
			    	<include name="*.hbm.xml"/>
				</fileset>
			</configuration>
			<hbm2ddl export="false" outputfilename="create-tables.sql" />

		</hibernatetool>
	</target>
</project>
[xml]</pre>
</pre>
<p>In the above xml I declared the hibernatetool task via taskdef. Secondly, I created a target called <code>generate-sql</code> that I am going to call from the Maven build. The hibernatetool and hbm2ddl tasks are documented on the hibernate tools page, so checkout that if you need more info on how to configure.<br />
As you can see I am using <code>${}</code> properties. These are passed in from the Maven configuration. The file <code>hibernate-for-sql-generation.properties</code> contains the Dialect that Hibernate will use to generate the sql file.</p>
<p>Next is the relevant part of the pom.xml. Since I don't want to generate the sql each time I do a Maven build, I added it into a profile:</p>
<pre class="brush: xml;">[/xml]
<profile>
	<id>generate-sql</id>
	<build>
<plugins>
<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.3</version>
				<executions>
					<execution>
						<id>generate-sql</id>
<phase>compile</phase>
						<configuration>
							<tasks>
<property name="test_classpath" refid="maven.test.classpath" />
<property name="hibernate_dir" value="${basedir}/src/main/resources/hibernate" />
<property name="hibernate_testdir" value="${basedir}/src/test/resources" />
<property name="output_dir" value="${basedir}/target/database" />
								<ant antfile="${basedir}/build.xml">
									<target name="generate-sql" />
								</ant>
							</tasks>
								</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</profile>
[xml]</pre>
<p>The really interesting part starts from the <em>execution</em> tag. In here I tell the maven-antrun-plugin what to do. As you can see I pass in, amongst other things, the Maven test classpath. The classpath is needed by Ant to locate the <code>org.hibernate.tool.ant.HibernateToolTask</code>. Since I pass in the test classpath I added the hibernatetools jar as a dependency with scope test in the pom.xml.</p>
<p>When you now run <code>mvn compile -Pgenerate-sql </code> it will generate a sql file in the directory <code>target/database</code>. With some minor adjustments you are ready to use the generated file for creating your tables on all environments.</p>
<p>Lars</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.larsvonkconsultancy.nl/2008/12/generating-sql-files-in-maven-using-hibernate-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
