Friday, October 31, 2008

Configuring Cargo/Tomcat with Maven2

Commenting on my last post, Rob Sinner asked, "Can you post the pom.XML your using to launch cargo[?]"  I haven't had the problem he described, but I don't mind sharing the configuration I use.


Basic Cargo/Tomcat Configuration
A basic cargo/tomcat configuration adds the cargo plugin to your build plugins. If you place this within a WAR module, the module in which the cargo plugin is defined will be automatically deployed with no extra work on your part:

<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0-beta-1</version>
<configuration>
<container>
<containerId>tomcat5x</containerId>
<zipUrlInstaller>
<url>http://www.apache.org/dist/tomcat/tomcat-5/v5.5.27/bin/apache-tomcat-5.5.27.zip</url>
</zipUrlInstaller>
</container>
<configuration>
<home>${project.build.directory}/tomcat5x/</home>
<properties>
<cargo.servlet.port>${servlet.port}</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
To fire up cargo, simply enter:
mvn cargo:start
To exit, press Ctrl-C.

Integration Tests
You can configure Cargo to start before the integration test phase and shut down after the integration test phase. This is useful if you have automated integration tests that you'd like to run against your in-container application. It's as simple as binding the cargo goals to the correct lifecycle phases:

<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<wait>false</wait>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>

Deploying Other Modules
In some cases, you'll want to deploy one or more modules that aren't the project you're in. In this case, you'll want to define deployables explicitly inside the cargo configuration:

<deployables>
<deployable>
<groupId>com.mycompany.myproject</groupId>
<artifactId>myproject-alpha</artifactId>
<type>war</type>
<properties>
<context>alpha</context>
</properties>
</deployable>
<deployable>
<groupId>com.mycompany.myproject</groupId>
<artifactId>myproject-beta</artifactId>
<type>war</type>
<properties>
<context>beta</context>
</properties>
</deployable>
</deployables>
Debugging
If you want the option of connecting a remote debugger to tomcat after firing it up via Maven/Cargo, I'd suggest a profile.  This is what we typically use:

<profile>
<id>cargo-debug</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0-alpha-6</version>
<configuration>
<configuration>
<properties>
<cargo.jvmargs><![CDATA[-Xdebug -Xrunjdwp:transport=dt_socket,address=15102,server=y,suspend=n]]></cargo.jvmargs>
</properties>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
</profile>

You can invoke this using:
mvn cargo:start -Pcargo-debug

Alternatives
There's also a Maven Jetty Plugin which has a pretty good reputation.  Unfortunately, there are currently two plugins with the jetty prefix, one in the mojo repository, and the other from mortbay.  The former takes precedence for me, whicih makes using the Mortbay version painful.

No comments: