Tuesday, August 26, 2008

Jetty-Maven-Eclipse ... mmmmm nice

Here's on how to setup jetty:run goal for a maven project, and to run it from eclipse.

Configure Jetty in project's pom.xml

The first step is to add maven-jetty-plugin to you project's pom.xml. Here is n extract from my sample project.



<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
<jettyEnvXml>src/main/resources/jetty-env.xml</jettyEnvXml>
<webApp>target/webmeet-1.0</webApp>
</configuration>
</plugin>


No rocket science here, the configuration tag defines the configuration for your specific needs i will elaborate it a little later.

  • <scanIntervalSeconds> tag defines the interval after which the plugin will 'scan' all resources in 'webb-application' base directory for changes. Good option if you will be editing your JSPs or VMs(velocity!) on the fly while developing and want to see immediate results.
  • <jettyEnvXml> tag defines the XML where you configure environment properties for Jetty. For example 'propertyhosts' and JNDI data-sources etc
  • <webApp> tag points to the 'exploded' webapp created by maven install goal in your eclipse project.

Adding Dependencies

If needed you can add dependencies 'selectively' toyour pom so that they apply only on the jetty configuration. Here's how to do that


<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
<type>jar</type>
</dependency>
.....

</dependencies>

The dependencies tag should be nested within the plugin tag.

The Jetty Environmental Config

Environmental resources such as JNDI names etc for datasources can be configured in a file pointed by the < jettyEnv > tag. In my sample I configure a propertyhost and a datasource. Following is the complete xml file


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New class="org.mortbay.jetty.plus.naming.EnvEntry">
<Arg>propertyhost</Arg>
<Arg type="java.lang.String">localhost:22081/</Arg>
</New>

<New id="webmeet" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>jdbc/webmeet</Arg>
<Arg>
<New class="oracle.jdbc.pool.OracleConnectionPoolDataSource">
<Set name="Password">password</Set>
<Set name="User">appuser</Set>
<Set name="URL">
jdbc:oracle:thin:@127.0.0.1:1521:XE
</Set>
</New>
</Arg>
</New>
</Configure>

To run this from command line run following command from the maven project root

mvn jetty:run  

Running from Eclipse

Eclipse 3.3.X Europa

  • Choose Run > Open Run Dialog.
  • Select Maven Build and click on add new configuration button
  • Give the configuration a name
  • Add goal jetty:run
  • If you want to use log4j instead of the sl4j provided by jetty add the following in the WM argson the JRE tab
    -Dslf4j=false -Dlog4j.configuration=file:path/to/your.log4j.properties
  • Click 'Apply' and click 'Run' to run the webapp in Jetty.

Eclipse 3.2.X Callisto

The steps are same as above except that you have to choose Run > External Tools > Open External Tool Dialog in the first step.

No comments: