Monday, December 5, 2011

Runnig Jetty from Maven with JNDI Data Source

During the development of a java web component (aka war) it can be very useful to run the application as quick as possible. Jetty provides a Maven plugin that allows running it directly from maven build or explicitly using "mvn jetty:run" from the command line.

But what happens if the war uses external database? Especially when the datasource is defined externally to the war and accessed vie the JNDI?

Actually the solution if quite simple:

Step 1 - Define the Data-source

Create the file defining the data-source:
(The example below is for Oracle. See this page for examples of the other databases.
<Configure id="Server" class="org.eclipse.jetty.server.Server">
    <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg></Arg>
        <Arg>jdbc/my_ds</Arg>
        <Arg>
            <New class="oracle.jdbc.pool.OracleDataSource">
                <Set name="DriverType">thin</Set>
                <Set name="URL">jdbc:oracle:thin:@(DESCRIPTION=(ENABLE=BROKEN)(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=something_to_replace)))
                </Set>
                <Set name="User">my_user</Set>
                <Set name="Password">my_password</Set>
                <Set name="connectionCachingEnabled">true</Set>
                <Set name="connectionCacheProperties">
                    <New class="java.util.Properties">
                        <Call name="setProperty">
                            <Arg>MinLimit</Arg>
                            <Arg>10</Arg>
                        </Call>
                        <Call name="setProperty">
                            <Arg>InactivityTimeout</Arg>
                            <Arg>600</Arg>
                        </Call>
                    </New>
                </Set>
            </New>
        </Arg>
    </New>
</Configure>

Pay attention to "jdbc/my_ds". It's the datasource JNDI name. Make sure to put there the actual JNDI name used by your application.

Place this file under your maven project. In my opinion the best is to place this file under src/dev/resources, but basically it can be anywhere.

To continue the example, I'll name the file: jetty-ds-dev.xml

Step 2 - Define the Jetty Maven Plugin

<build>
...
        <plugins>
...
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty-version}</version>
                <configuration>
                    <jettyConfig>src/dev/resources/jetty-ds-dev.xml</jettyConfig>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc14</artifactId>
                        <version>${oracle-ojdbc-version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Pay attention to the configuration of jetty-ds-dev.xml.
Also pay attention that Jetty must be able to find the relevant JDBC driver in its classpath!

And basically that's all. Run "mvn jetty:run" and your application should work with the provided database.


Recommended Reading

1. Apache Maven 3 Cookbook
2. Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation

No comments: