Thursday, 2 May 2013

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:

Now, I needed to define the server admin credentials in my maven settings (~/.m2/settings.xml):

Then, I updated the POM to configure the maven tomcat plugin:


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).

6 comments:

  1. very nice.. thanks
    Can we have multiple configurations so that we can deploy to say qa and dev box with different commands?

    ReplyDelete
  2. Excellent Thanks for this information. this is very wonderful data about tomcat hosting server.

    ReplyDelete
  3. Beautiful example, better than books simple and straight to the point. solved a deployment issue that toke my entire day. Many thanks

    ReplyDelete
  4. A small addition, you need also to close any antivirus for it to work otherwise the deploy will fail.

    ReplyDelete
  5. I am getting unauthorized error. The password is correct. URL is correct. But not sure why the error is? Any idea? HERE is my SO post. http://stackoverflow.com/questions/23272360/tomcat-maven-plugin-unauthorized-error

    ReplyDelete