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:
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:
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<settings> | |
<servers> | |
<server> | |
<id>dev-tomcat</id> | |
<username>tomcat</username> | |
<password>password</password> | |
</server> | |
</servers> | |
</settings> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
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).