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 :
- Maven 3
- Tomcat 6.0.37
- Tomcat 7.0.53
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
.
<?xmlversion='1.0'encoding='utf-8'?><tomcat-users> <rolerolename="manager-gui"/><rolerolename="manager-script"/><userusername="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.
<?xml version="1.0"encoding="UTF-8"?><settings ...><servers> <server><id>TomcatServer</id><username>admin</username><password>password</password></server> </servers></settings>
1.3 Tomcat7 Maven Plugin
Declares a Maven Tomcat plugin.
<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]------------------------------------------------------------------------

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
<?xmlversion='1.0'encoding='utf-8'?><tomcat-users> <rolerolename="manager-gui"/><rolerolename="manager-script"/><userusername="admin"password="password"roles="manager-gui,manager-script"/> </tomcat-users>
2.2 Maven Authentication
<?xml version="1.0"encoding="UTF-8"?><settings ...><servers> <server><id>TomcatServer</id><username>admin</username><password>password</password></server> </servers></settings>
2.3 Tomcat6 Maven Plugin
<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
... [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 ] ------------------------------------------------------------------------