<?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; Maven</title>
	<atom:link href="http://blog.larsvonkconsultancy.nl/tag/maven/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>
		<item>
		<title>Maven and Jetty: A Perfect fit</title>
		<link>http://blog.larsvonkconsultancy.nl/2008/11/maven-and-jetty-a-perfect-fi/</link>
		<comments>http://blog.larsvonkconsultancy.nl/2008/11/maven-and-jetty-a-perfect-fi/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 21:50:14 +0000</pubDate>
		<dc:creator>Lars Vonk</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[jetty]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://lvonk.wordpress.com/?p=5</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin" onclick="pageTracker._trackPageview('/outgoing/docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin?referer=');">jetty-maven-plugin</a> is a real life saver when it comes to developing web applications. In the past I had such horrible experiences with the eclipse WTP plugin that I banned it from my toolset forever.<br />
For a year now I use the jetty-maven-plugin and it makes developing so much easier. In this blog I will give an overview how you can setup your development environment using Maven and Jetty.<br />
<span id="more-5"></span><br />
When developing webapps on my local machine I set myself the following requirements:</p>
<p>1. I want to be able to edit classes and views (Wicket html files or jsp&#8217;s) and see the results in the browser fast without having to build a war file and manually redeploy.<br />
2. I want to be able to switch between an in memory database, typically a Hsqldb and the real target database (Oracle). Since I am using Hibernate as ORM I also need to be able to switch settings like the <i>hibernate.dialect</i> property to match my database.<br />
3. The WAR file produced should also run on the target application server, JBoss in my case.</p>
<p>Let&#8217;s see how Jetty fulfills these requirements.</p>
<p>The example project consist of a webapplication using Spring, Hibernate and Wicket. The target platform is JBoss with an Oracle database.</p>
<p>To get started we first need to add the jetty-maven-plugin in the pom.xml of the war project.</p>
<pre class="brush: xml;">[/xml]
<plugin>
  <artifactId>jetty-maven-plugin</artifactId>
	<groupId>org.mortbay.jetty</groupId>
	<version>7.0.0pre3</version>
	<configuration>
		<contextPath>/example-app</contextPath>
		<scanIntervalSeconds>3</scanIntervalSeconds>
		<connectors>
			<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8090</port>
				<host>0.0.0.0</host>
			</connector>
		</connectors>
	</configuration>
</plugin>
[xml]</pre>
<p>That&#8217;s it. When running <i>mvn jetty:run</i> the webapplication is available under http://localhost:8090/example-app. For a complete reference of all possible configuration settings I suggest you read <a href="http://jetty.mortbay.org/jetty/maven-plugin/run-mojo.html" onclick="pageTracker._trackPageview('/outgoing/jetty.mortbay.org/jetty/maven-plugin/run-mojo.html?referer=');">http://jetty.mortbay.org/jetty/maven-plugin/run-mojo.html</a></p>
<p><b>Fast redeploying.</b><br />
In my experience, I have no evidence, Jetty is the fastest starting servlet container out there, especially when you compare it with full blown application servers such as JBoss. JBoss is perfect for production and acceptance environments but when developing I have different needs. This is where Jetty gets a big plus.<br />
Regarding redeploying: By default the jetty-maven-plugin automatically reloads/redeploys the webapplication whenever it detects a change. You can use the <i>scanIntervalSeconds</i> and <i>scanTargets</i> configuration properties to tweak change detection. Downside of this automatic reloading is that it will always reload, even if you are just developing for a couple of minutes and save changes in between. Not only does this consume unnecessary resources, it will eventually lead to full PermGen space which results in an OOME.<br />
A way to counter this is to set the <a href="http://jetty.mortbay.org/jetty/maven-plugin/run-mojo.html#reload" onclick="pageTracker._trackPageview('/outgoing/jetty.mortbay.org/jetty/maven-plugin/run-mojo.html_reload?referer=');">reload</a> option of the jetty-maven-plugin to <i>manual</i>. Now reloading will only occur on a linefeed (hitting enter) in the console where you started jetty.</p>
<p><b>Switching databases</b><br />
When developing a webapplication I want to be flexible. I want to be able to connect to a fast in memory database, to the target database and to a database that is for instance on a test machine. How can we achieve this with jetty? In my case I have two local databases: a Hsql database and an Oracle database installed on a VMWare image.<br />
Since my Spring context uses a JNDI configured datasource I need to add those in Jetty as well. For this you need to create set the <i>jettyEnvXml</i> property in the jetty-maven-plugin. This property points to the jetty configuration file, called <a href="http://docs.codehaus.org/display/JETTY/jetty-env.xml" onclick="pageTracker._trackPageview('/outgoing/docs.codehaus.org/display/JETTY/jetty-env.xml?referer=');">jetty-env.xml</a>, containing the datasource in our case. But we also need to be able to switch hibernate dialect. Since we are using Spring we have the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html" onclick="pageTracker._trackPageview('/outgoing/static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html?referer=');"><i>PropertyPlaceHolderConfigurer</i></a> in our applicationContext in <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html#SYSTEM_PROPERTIES_MODE_OVERRIDE" onclick="pageTracker._trackPageview('/outgoing/static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html_SYSTEM_PROPERTIES_MODE_OVERRIDE?referer=');">SYSTEM_PROPERTIES_MODE_OVERRIDE</a> mode. This means that system properties override values defined in the applicationContext or the property file (if any). Together with Maven&#8217;s profiles we can now configure the jetty-maven-plugin so we can switch databases as needed.</p>
<p>The relevant parts of our pom now look like this:</p>
<pre class="brush: xml;">[/xml]
...
<plugin>
  <artifactId>jetty-maven-plugin</artifactId>
	<groupId>org.mortbay.jetty</groupId>
	<version>7.0.0pre3</version>
	<configuration>
		<contextPath>/example-app</contextPath>
		<reload>manual</reload>
                <jettyEnvXml>${jetty.env.file}</jettyEnvXml>
		<connectors>
			<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8090</port>
				<host>0.0.0.0</host>
			</connector>
		</connectors>
		<systemProperties>
			<systemProperty>
				<name>hibernate.dialect</name>
				<value>${hibernate.dialect}</value>
			</systemProperty>
		</systemProperties>
	</configuration>
</plugin>
...
<profiles>
<profile>
		<id>jetty-oracle</id>
<properties>
			<jetty.env.file>${basedir}/src/test/resources/jetty-env-oracle.xml</jetty.env.file>
			<hibernate.dialect>org.hibernate.dialect.Oracle10gDialect</hibernate.dialect>
		</properties>
	</profile>
<profile>
		<id>jetty-hsql</id>
<properties>
			<jetty.env.file>${basedir}/src/test/resources/jetty-env-hsqldb.xml</jetty.env.file>
			<hibernate.dialect>org.hibernate.dialect.HSQLDialect</hibernate.dialect>
		</properties>
	</profile>
</profiles>
[xml]</pre>
<p>The contents of the jetty-env-hsqldb.xml is as follows:</p>
<pre class="brush: xml;">[/xml]
<?xml version="1.0"  encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="wac" class="org.mortbay.jetty.webapp.WebAppContext">
	<New id="example" class="org.mortbay.jetty.plus.naming.Resource">
		<Arg>jdbc/example</Arg>
		<Arg>
			<New class="org.apache.commons.dbcp.BasicDataSource">
				<Set name="driverClassName">org.hsqldb.jdbcDriver</Set>
				<Set name="url">jdbc:hsqldb:mem:test</Set>
				<Set name="username">sa</Set>
				<Set name="password"></Set>
			</New>
		</Arg>
	</New>
</Configure>
[xml]</pre>
<p>Via this configuration we are able to switch between Oracle and Hsqldb by specifying the profile at the commandline. For example <i>mvn jetty:run -Pjetty-hsql</i> in case we want the Hsqldb.</p>
<p><b>Servlet container compatibility</b><br />
We are almost there. As said before, the example application is using a JNDI configured datasource and refers to it in the applicationContext using a jee:jndi lookup:</p>
<pre class="brush: xml;">[/xml]
<jee:jndi-lookup id="exampleDataSource" jndi-name="java:comp/env/jdbc/example"/>
[xml]</pre>
<p>When deploying the produced WAR file in JBoss it gives an error saying that <i>jdbc is not bound<i>. To get the application up and running in JBoss as well you to follow the steps as described in <a href="http://forums.sun.com/thread.jspa?threadID=626778" onclick="pageTracker._trackPageview('/outgoing/forums.sun.com/thread.jspa?threadID=626778&amp;referer=');">http://forums.sun.com/thread.jspa?threadID=626778</a>. This is caused by the fact that not all servlet containers have the same way of registering jndi values.</p>
<p><b>Conclusion</b><br />
Jetty and Maven go well together. The jetty-maven-plugin provides an easy way to use Jetty in development and enough flexibility to switch for instance databases when developing. I gihly recommend using the jetty-maven-plugin when developing. But make sure you check that the WAR file also runs on your target application server on a daily basis.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.larsvonkconsultancy.nl/2008/11/maven-and-jetty-a-perfect-fi/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
