Wednesday, October 19, 2011

Adding a datasource to the maven jetty plugin

In your pom.xml
           <plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.21</version>
<configuration>
<!-- <jettyConfig>src/main/resources/devJetty.xml</jettyConfig> -->
<webXml>${basedir}/src/main/resources/jetty-web.xml</webXml>
<jettyEnvXml>${basedir}/src/main/resources/jetty-env.xml</jettyEnvXml>
<contextPath>/OCM</contextPath>
<scanIntervalSeconds>3</scanIntervalSeconds>
<scanTargetPatterns>
<scanTargetPattern>
<directory>src/main/webapp/WEB-INF</directory>
<excludes>
<exclude>**/*.jsp</exclude>
</excludes>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</scanTargetPattern>
</scanTargetPatterns>
<stopKey>foo</stopKey>
<stopPort>9966</stopPort>
</configuration>
</plugin>
Key lines are
<webXml>${basedir}/src/main/resources/jetty-web.xml</webXml>
<jettyEnvXml>${basedir}/src/main/resources/jetty-env.xml</jettyEnvXml>

Define your jetty-env.xml like this:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC -//Mort Bay Consulting//DTD Configure//EN http://jetty.mortbay.org/configure.dtd>

<Configure class="org.mortbay.jetty.webapp.WebAppContext">

<!-- an XADataSource -->
<New id="MAVENJETTYDS" class="org.mortbay.jetty.plus.naming.Resource">
<Arg></Arg>
<Arg>jdbc/datasource</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">[[jdbc driver class]]</Set> <!-- for example, com.ibm.db2.jcc.DB2Driver -->
<Set name="url">[[jdbc url]]</Set> <!-- for example, jdbc:db2://yourdbhostnameorip:[port]/yourdb -->
<Set name="username">[[username]]</Set><!-- for example, yourmom -->
<Set name="password">[[password]]</Set><!-- for example, correcthorsebatterystaple -->
<Set name="defaultCatalog">[[schema, if needed]]</Set>
</New>
</Arg>
</New>
</Configure>

Then add the ref to your web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_1270163143862">
<pre>
<!-- SNIP -->

<!-- Bug fix for editing locked CSS and JS running in Jetty -->
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
<init-param>
<param-name>useFileMappedBuffer</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

<resource-ref>
<res-ref-name>jdbc/datasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

</web-app>

No comments:

About Me

My photo
Lead Java Developer Husband and Father

Tags