Deploying to Tomcat 7 with Maven

It's sometimes nice to be able to update a development Tomcat 7 server from Maven - this makes it simple to hook automatic deployment into a CI server or just update the dev server as a developer.

First, the Tomcat manager application needs to be installed (check Tomcat's webapps directory for the manager application) and configured with the appropriate user credentials:

<?xml version='1.0' encoding='cp1252'?>
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="tomcat" password="password" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>
</tomcat-users>
view raw gistfile1.xml hosted with ❤ by GitHub
Now, I needed to define the server admin credentials in my maven settings (~/.m2/settings.xml):

<settings>
<servers>
<server>
<id>dev-tomcat</id>
<username>tomcat</username>
<password>password</password>
</server>
</servers>
</settings>
view raw gistfile1.xml hosted with ❤ by GitHub
Then, I updated the POM to configure the maven tomcat plugin:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<build>
...
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<!-- Tomcat 7 has a different manager path - text - than other versions -->
<url>http://REMOTE_SERVER_NAME_GOES_HERE:8080/manager/text</url>
<!-- Refer to the server settings in your ~/.m2/settings.xml -->
<server>dev-tomcat</server>
<path>/app1</path>
</configuration>
</plugin>
</plugins>
...
</build>
view raw gistfile1.xml hosted with ❤ by GitHub

Now, using 'mvn tomcat7:redeploy' lets me update the dev server.

Note however, on Windows you may have some problems with undeploying the application - after an undeploy command, some jars may be left over in the webapps/appname directory. When you try to redeploy your app you'll see an error containing "cannot invoke tomcat manager fail - unable to delete...".

To work around this, you can change the TOMCAT_HOME/conf/context.xml to include the 'antiJARLocking' attribute like so:
<Context antiJARLocking="true">
The documentation points out though that this will impact start up times of applications.

In my case, I noticed problems when doing a redeploy to Tomcat - most likely unrelated to Maven and/or the maven tomcat plugin and more to do with PermGen (I saw perm gen OutOfMemory: PermGen space errors in the tomcat7-stderr logs, and the Tomcat process was consuming 100% cpu). Adding the following switches to the Tomcat JVM settings seems to have fixed it for now:
-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -XX:PermSize=64m -XX:MaxPermSize=128m -Xmx992M
 (In this instance, I'm running Tomcat 7 as a Windows service on JDK7 with a 50MB WAR file).

Popular posts from this blog

AspectJWeaver with JDK 7 - error - only supported at Java 5 compliance level or above

Intellij tip - Joining multiple lines into one

JUnit parameterized test with Spring autowiring AND transactions