In this tutorial, we will show you how to use Maven-Tomcat plugin to package and deploy a WAR file to Tomcat, both in Tomcat 6 and 7.

Libraries used :

  1. Maven 3
  2. Tomcat 6.0.37
  3. Tomcat 7.0.53
Tomcat 7
Deploy URL = http://localhost:8080/manager/text
Command = mvn tomcat7:deploy

Tomcat 6
Deploy URL = http://localhost:8080/manager/
Command = mvn tomcat6:deploy

1. Tomcat 7 Example

This example shows you how to package and deploy a WAR file on Tomcat 7.

1.1 Tomcat Authentication
Add an user with roles manager-gui and manager-script.

%TOMCAT7_PATH%/conf/tomcat-users.xml

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>

	<role rolename="manager-gui"/>
	<role rolename="manager-script"/>
	<user username="admin" password="password" roles="manager-gui,manager-script" />

</tomcat-users>

1.2 Maven Authentication
Add above Tomcat’s user in the Maven setting file, later Maven will use this user to login Tomcat server.

%MAVEN_PATH%/conf/settings.xml



	
	   
		
			TomcatServer
			admin
			password
		

	

1.3 Tomcat7 Maven Plugin
Declares a Maven Tomcat plugin.

pom.xml

	<plugin>
		<groupId>org.apache.tomcat.maven</groupId>
		<artifactId>tomcat7-maven-plugin</artifactId>
		<version>2.2</version>
		<configuration>
			<url>http://localhost:8080/manager/text</url>
			<server>TomcatServer</server>
			<path>/mkyongWebApp</path>
		</configuration>
	</plugin>

How it works?
During deployment, it tells Maven to deploy the WAR file to Tomcat server via “http://localhost:8080/manager/text” , on path “/mkyongWebApp“, using “TomcatServer” (in settings.xml) username and password for authentication.

1.4 Deploy to Tomcat
Commands to manipulate WAR file on Tomcat.


mvn tomcat7:deploy 
mvn tomcat7:undeploy 
mvn tomcat7:redeploy 

Example


> mvn tomcat7:deploy

...
[INFO] Deploying war to http://localhost:8080/mkyongWebApp
Uploading: http://localhost:8080/manager/text/deploy?path=%2FmkyongWebApp&update=true
Uploaded: http://localhost:8080/manager/text/deploy?path=%2FmkyongWebApp&update=true (13925 KB at 35250.9 KB/sec)

[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /mkyongWebApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.507 s
[INFO] Finished at: 2014-08-05T11:35:25+08:00
[INFO] Final Memory: 28M/308M
[INFO] ------------------------------------------------------------------------
maven-war-file-deploy-tomcat7

2. Tomcat 6 Example

This example shows you how to package and deploy a WAR file on Tomcat 6. The steps are same with Tomcat 7, just the deploy url and command name are different.

2.1 Tomcat Authentication

%TOMCAT6_PATH%/conf/tomcat-users.xml

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>

	<role rolename="manager-gui"/>
	<role rolename="manager-script"/>
	<user username="admin" password="password" roles="manager-gui,manager-script" />

</tomcat-users>

2.2 Maven Authentication

%MAVEN_PATH%/conf/settings.xml



	
	   
		
			TomcatServer
			admin
			password
		

	

2.3 Tomcat6 Maven Plugin

pom.xml

	<plugin>
		<groupId>org.apache.tomcat.maven</groupId>
		<artifactId>tomcat6-maven-plugin</artifactId>
		<version>2.2</version>
		<configuration>
			<url>http://localhost:8080/manager</url>
			<server>TomcatServer</server>
			<path>/mkyongWebApp</path>
		</configuration>
	</plugin>			

2.4 Deploy to Tomcat


mvn tomcat6:deploy 
mvn tomcat6:undeploy 
mvn tomcat6:redeploy 

Example


> mvn tomcat6:deploy

...
[INFO] Deploying war to http://localhost:8080/mkyongWebApp
Uploading: http://localhost:8080/manager/deploy?path=%2FmkyongWebApp
Uploaded: http://localhost:8080/manager/deploy?path=%2FmkyongWebApp (13925 KB at 32995.5 KB/sec)

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.652 s
[INFO] Finished at: 2014-08-05T12:18:54+08:00
[INFO] Final Memory: 30M/308M
[INFO] ------------------------------------------------------------------------

References

  1. Apache Tomcat 7 Manager App HOW-TO
  2. Apache Tomcat 6 Manager App HOW-TO
  3. Tomcat Maven Plugin
  4. Tomcat Maven Plugin – Context Goals