Wednesday, January 23, 2013

jetty maven plugin

maven: v3.x
maven jetty plugin : 7.6.3.v20120416
install m2eclipse plugin

1. add jetty dependency in pom.xml

<build>
<!-- jetty maven plugin -->
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.6.3.v20120416</version>
<configuration>
<jettyXml>src/test/resources/jetty-plus.xml</jettyXml>
<!-- scan project folder to check if any changes, restart jetty if any changes found -->
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppConfig>
<contextPath>/demo</contextPath>
</webAppConfig>
<stopPort>9966</stopPort>
<stopKey>stop</stopKey>
<systemProperties>
<systemProperty>
<name>log4j.configurationFile</name>
<value>src/main/resources/log4j.properties</value>
</systemProperty>
</systemProperties>
</configuration>
<dependencies>
<!-- modules dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- maven plugin to skip test while building -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>

2. open 'Run Configurations...' dialogue in eclipse
2.1 under menu 'Maven Build' to create a new task 'jetty-stop'













2.2 create a new task 'jetty-debug'




























3. configure JNDI in jetty
take note the configuration highlighted in pom.xml

This is the JNDI settings in jetty-plus.xml


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

<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Array id="plusConfig" type="java.lang.String">
<Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
<Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item> <!-- add for JNDI -->
<Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>    <!-- add for JNDI -->
<Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
<Item>org.eclipse.jetty.webapp.TagLibConfiguration</Item>
</Array>

<Call name="setAttribute">
<Arg>org.eclipse.jetty.webapp.configuration</Arg>
<Arg>
<Ref id="plusConfig" />
</Arg>
</Call>

<!-- binding Jetty JNDI datasource to jdbc/mydatasource -->
<New id="integration_datasource_jndi" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>
<Ref id="Server" />
</Arg>
<Arg>jdbc/demodb</Arg>
<Arg>
<New class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<Set name="driverClassName">com.mysql.jdbc.Driver</Set>
<Set name="url">jdbc:mysql://localhost:3306/demodb</Set>
<Set name="username">{userid}</Set>
<Set name="password">{password}</Set>
</New>
</Arg>
</New>
</Configure>

and in web.xml , need configure <resource-ref> element as follows,


<resource-ref>
<description>JNDI config to access MySQL Database</description>
<res-ref-name>jdbc/demodb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>



No comments:

Post a Comment