In this tutorial, we will learn how to package and deploy a WAR file to Tomcat using Maven’s Tomcat plug-in (Tomcat 6 and 7).

Tools to use:

  1. Maven 3
  2. Tomcat 6.0.37
  3. Tomcat 7.0.53
Tomcat 7


Release the URL =

http://localhost:8080/manager/text


Command = MVN tomcat7:deploy

Tomcat 6 release URL =

http://localhost:8080/manager/


Command = MVN tomcat6:deploy

1. Tomcat 7 example

This example shows how to package and deploy a WAR file in Tomcat7.

1.1 Tomcat Authentication Add users with role manager GUI and management scripts.

%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>Copy the code


1.2 Maven Authentication The Tomcat user added to the Maven Settings file above is used by Maven to log in to the 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>Copy the code


Tomcat7 Maven Plugin Declares a Tomcat plugin for Maven.

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>/yiibaiWebApp</path> </configuration> </plugin>Copy the code


How does it work? During deployment, it tells Maven to deploy a WAR file Tomcat server, “Http://localhost:8080/manager/text”, “the path/yiibaiWebApp”, use “TomcatServer” (Settings. XML) user name and password for authentication.

1.4 The following commands published to Tomcat are used to manipulate the Tomcat WAR file.

mvn tomcat7:deploy 
mvn tomcat7:undeploy 
mvn tomcat7:redeployCopy the code

The sample

> mvn tomcat7:deploy ... [INFO] Deploying war to http://localhost:8080/yiibaiWebApp Uploading: http://localhost:8080/manager/text/deploy? path=%2FyiibaiWebApp&update=trueUploaded: http://localhost:8080/manager/text/deploy? path=%2FyiibaiWebApp&update=true(13925 KB at 35250.9 KB/ SEC) [INFO] tomcatManager status code:200, ReasonPhrase:OK [INFO] OK - Deployed application at context path /yiibaiWebApp [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [INFO] Total time: 8.507 s [INFO] Finished at: 2015-08-05T11:35:25+08:00 [INFO] Final Memory: 28M/308M [INFO] ------------------------------------------------------------------------Copy the code

Complete project source code