Close

Maven - Using tomcat-maven-plugin to deploy war file to Tomcat server

[Last Updated: Jan 9, 2018]

This example demonstrates how to use tomcat7-maven-plugin to deploy project war from command line without using tomcat application manager page, http://localhost:8080/manager/html.

Currently there exists no Tomcat 8 or 9 versions of tomcat maven plugin, but Tomcat7-maven-plugin works for other versions as well.

In this example we are going to use Apache Tomcat Version 9.0.0.M15 and tomcat7-maven-plugin version 2.2.


Creating a simple war project

Creating a Servlet

we are going to use Servlet 3.1 API:

@WebServlet(name = "testServlet",
        urlPatterns = {"/test"},
        loadOnStartup = 1)
public class TestServlet extends HttpServlet {

  @Override
  protected void doGet (HttpServletRequest req,
                        HttpServletResponse resp)
            throws ServletException, IOException {

      resp.setContentType("text/html");
      PrintWriter writer = resp.getWriter();
      writer.println("test message");
  }
}

We don't have to add web.xml in Servlet 3.1.

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicbig.example</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</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>
<username>joe</username>
<password>joe</password>
</configuration>
</plugin>
</plugins>
</build>
</project>

Creating user and password in tomcat

<tomcat-home>/conf/tomcat-users.xml

<tomcat-users ....>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
	<user username="joe" password="joe" roles="manager-gui,manager-script"/>
</tomcat-users>

Running tomcat server

Click on:

<tomcat-home>/bin/startup.bat

Deploy to the server:

Go to the project root and enter:

mvn tomcat7:deploy

D:\examples\tomcat-maven-plugin>mvn tomcat7:deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building tomcat-maven-plugin 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:deploy (default-cli) > package @ tomcat-maven-plugin >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ tomcat-maven-plugin ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\examples\tomcat-maven-plugin\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ tomcat-maven-plugin ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ tomcat-maven-plugin ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\examples\tomcat-maven-plugin\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ tomcat-maven-plugin ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ tomcat-maven-plugin ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:3.0.0:war (default-war) @ tomcat-maven-plugin ---
[INFO] Packaging webapp
[INFO] Assembling webapp [tomcat-maven-plugin] in [D:\examples\tomcat-maven-plugin\target\tomcat-maven-plugin-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [25 msecs]
[INFO] Building war: D:\examples\tomcat-maven-plugin\target\tomcat-maven-plugin-1.0-SNAPSHOT.war
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:deploy (default-cli) < package @ tomcat-maven-plugin <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:deploy (default-cli) @ tomcat-maven-plugin ---
[INFO] Deploying war to http://localhost:8080/tomcat-maven-plugin
Uploading: http://localhost:8080/manager/text/deploy?path=%2Ftomcat-maven-plugin
Uploaded: http://localhost:8080/manager/text/deploy?path=%2Ftomcat-maven-plugin (3 KB)

[INFO] tomcatManager status code:200, ReasonPhrase:
[INFO] OK - Deployed application at context path /tomcat-maven-plugin
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.885 s
[INFO] Finished at: 2017-01-27T15:43:55-06:00
[INFO] Final Memory: 16M/251M
[INFO] ------------------------------------------------------------------------

D:\examples\tomcat-maven-plugin>

Checking web page


Redeploying

If there are some changes to the code then we just need to redeploy the war:

mvn tomcat7:redeploy

Undeploying

mvn tomcat7:undeploy

Also check out how to install Tomcat server here.




Example Project

Dependencies and Technologies Used:

  • javax.servlet-api 3.1.0 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Tomcat Maven Plugin Deploy Select All Download
  • tomcat-maven-plugin
    • src
      • main
        • java
          • com
            • logicbig
              • example
    • pom.xml

    See Also