Maven Tutorial - How To Deploy Maven Based War File To Tomcat








We can use Maven-Tomcat plugin to package and deploy a WAR file to Tomcat for both Tomcat 6 and 7.

We used the following libraries.

  • Maven 3
  • Tomcat 6.0.37
  • Tomcat 7.0.53




Command

For Tomcat 7, we have the following settings and command

Deploy URLhttp://localhost:8080/manager/text
Commandmvn tomcat7:deploy

For Tomcat 6 we use the following url and command

Deploy URLhttp://localhost:8080/manager/
Commandmvn tomcat6:deploy

Tomcat 7 Example

We can use the following steps to package and deploy a WAR file on Tomcat 7.

Add an user with roles manager-gui and manager-script in %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>

The we have to add above Tomcat's user in the Maven setting file (%MAVEN_PATH%/conf/settings.xml), 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>

Then add the Tomcat7 Maven Plugin to pom.xml in the plugin section

  <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>/java2sWebApp</path>
    </configuration>
  </plugin>

We can issue the following code to deploy WAR file to Tomcat.

The deploy command deploys the WAR file to Tomcat server via "http://localhost:8080/manager/text" , on path "/java2sWebApp", using "TomcatServer" in settings.xml username and password for authentication.

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





Tomcat 6 Example

We can use the following steps to deploy WAR file to Tomcat 6.

Add the following user name and role setting to %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>

Add the following Maven Authentication settings to %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>

Add the Tomcat6 Maven Plugin to POM.xml file


  <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>/java2sWebApp</path>
    </configuration>
  </plugin>

Use the following command to deploy to Tomcat

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