<?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; plugin</title>
	<atom:link href="http://blog.larsvonkconsultancy.nl/tag/plugin/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>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>
