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
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
<?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.
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>/favtutsWebApp</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 “/favtutsWebApp“, 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/favtutsWebApp
Uploading: http://localhost:8080/manager/text/deploy?path=%2FfavtutsWebApp&update=true
Uploaded: http://localhost:8080/manager/text/deploy?path=%2FfavtutsWebApp&update=true (13925 KB at 35250.9 KB/sec)
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /favtutsWebApp
[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
%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
<?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
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>/favtutsWebApp</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/favtutsWebApp
Uploading: http://localhost:8080/manager/deploy?path=%2FfavtutsWebApp
Uploaded: http://localhost:8080/manager/deploy?path=%2FfavtutsWebApp (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] ------------------------------------------------------------------------
Download Source Code
$ git clone https://github.com/favtuts/java-maven-tutorials.git
$ cd maven-war-tomcatTo run web application directly
$ mvn jetty:run
Access: http://localhost:8080To deploy Tomcat
$ mvn tomcat7:deploy
$ mvn tomcat6:deploy
Access: http://localhost:8181/helloWebApp