Sotirios-Efstathios Maneas

About Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

How to deploy Maven based war file to Tomcat example

In this tutorial, we will show you how to use the Apache Tomcat Maven plugin, in order to package and deploy a war file to Apache Tomcat. You can download the plugin from the official page.

In this example, we use the following tools on a Windows 7 platform:

  • Apache Maven 3.1.1
  • Apache Tomcat Maven Plugin
  • JDK 1.7
  • Apache Tomcat 7

 
Please refer to this guide here, in order to verify that you know how to create a Web application project, using Apache Maven.

1. Create a user in Apache Tomcat

First of all, we must create a user with administrator rights for the Apache Tomcat. Using the terminal (Linux or Mac) or the command prompt (Windows), navigate to the folder where the Apache Tomcat is installed. Then, navigate to the folder called conf and add the following code snippet to the tomcat-users.xml file, inside the <tomcat-users> tag:

<role rolename="manager"/>
<role rolename="admin"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="admin" roles="admin,manager,manager-gui,manager-script"/>

We have now successfully created a new user, called admin.

2. Create a user in Apache Maven

Using the terminal (Linux or Mac) or the command prompt (Windows), navigate to the folder where the Apache Maven is installed. Recall that the MAVEN_HOME environment variable points to the home folder of Maven. Then, navigate to the folder called conf and add the following code snippet to the settings.xml file, inside the servers tag:

<server>
   <id>TomcatServer</id>
   <username>admin</username>
   <password>admin</password>
</server>

We created a user with the name TomcatServer, along with its credentials.

3. Add Apache Tomcat Maven plugin as a Dependency

The next step is to declare the Apache Tomcat Maven plugin as a dependency to our project. Inside the pom.xml, we must add the following code snippet:

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>tomcat-maven-plugin</artifactId>
   <version>1.1</version>
   <configuration>
      <url>http://localhost:8080/manager/text</url>
      <server>TomcatServer</server>
      <path>/javaCodeGeeks</path>
   </configuration>
</plugin>

By using the above code snippet, we specify that our project must be deployed on http://localhost:8080/manager/text, in the path /javaCodeGeeks, using the credentials of the TomcatServer user.

A sample pom.xml file is shown below:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>com.javacodegeeks</groupId>
   <artifactId>SampleWebApplication</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>SampleWebApplication Maven Webapp</name>
   <url>http://maven.apache.org</url>

   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.11</version>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <finalName>SampleWebApplication</finalName>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
               <source>1.7</source>
               <target>1.7</target>
            </configuration>
         </plugin>

      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>tomcat-maven-plugin</artifactId>
         <version>1.1</version>
         <configuration>
            <url>http://localhost:8080/manager/text</url>
            <server>TomcatServer</server>
            <path>/javaCodeGeeks</path>
         </configuration>
      </plugin>
   </plugins>

   </build>
</project>

4. Deploy the project to Apache Tomcat

The first thing to do, before deploying our project, is start the Apache Tomcat. From the Windows command line, we execute the following command:

C:\tomcat\bin\startup.bat

Now, using the terminal (Linux or Mac) or the command prompt (Windows), we navigate to the folder where our project is located. Then, we issue the command:

mvn tomcat:deploy

This command packages our project in a war file and deploys it to the Apache Tomcat. In order to verify that our installation is successful, we must go to the manager page of the Apache Tomcat and check that /javaCodeGeeks actually exists:

http://localhost:8080/manager/html/:

mvn_tomcat_deploy_WM

Finally, if we want to deploy our project and the javaCodeGeeks.war file already exists, we must issue the following command:

mvn tomcat:redeploy

Notice that the Apache Tomcat must already be running, before we issue this command.
 
 
This was a tutorial on how to use the Apache Tomcat Maven plugin to deploy a war file to Apache Tomcat.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close