How To Deploy A Maven Project To Tomcat From Eclipse

After you create a maven project in eclipse for spring application development, you always need to use maven to clean, build, install and deploy the spring application to a tomcat server to see the result. This article will tell you how to deploy a maven eclipse project to tomcat.

1. Create Maven Project in STS ( Spring Tool Suite ).

First we should create a spring project in sts follow below steps. STS is a spring application IDE based on eclipse.

  1. Click File —> New —> Others menu, select Maven Project in the popup wizard dialog.
    eclipse file new others maven project wizard dialog
  2. Click Next button, check Create a simple project checkbox in the next dialog.
    new maven project check create a simple project new
  3. Input the spring project group id, artifact id and select packaging type with war in the next dialog.
    input maven group id artifact id packaging type new
  4. Click Finish button to complete the wizard. In the left package explorer panel, you can see the project just created, the project name is just the artifact id.
    eclipse maven project name is artifact id
  5. There is an error in the pom.xml, the error message is “web.xml is missing and <failOnMissingWebXml> is set to true“, so copy below xml code into pom.xml between project xml tag.
    <build>
        <finalName>Deploy maven project to tomcat example</finalName>
           <plugins>
              <plugin>
                 <artifactId>maven-war-plugin</artifactId>
                 <version>2.3</version>
                 <configuration>
                   <failOnMissingWebXml>false</failOnMissingWebXml>
                 </configuration>
              </plugin>
           </plugins>
    </build>
  6. Now right click the project name in left package explorer panel, click Maven —> Update Project menu item, then all the project error will disappear.
    sts click porject name maven update project menu

2. Deploy Maven Project To Tomcat.

  1. Before we can deploy eclipse maven project to tomcat, we need to add below user role in TOMCAT_HOME/conf/tomcat-users.xml file as below, otherwise there will throw an unauthorized error during the maven project deploy process. Then we should restart tomcat server.
    <!-- When deploy war to tomcat in maven plugin, the role should be manager-script -->
    <!-- tomcat role is used to deploy maven project to tomcat in eclipse scripts.-->
    <user username="tomcat" password="tomcat" roles="manager-script"/>
    <!-- admin role is used to login tomcat manager GUI. -->
    <user username="admin" password="admin" roles="manager-gui"/>
  2. Then add below xml in the pom.xml plugins 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>
          <path>/DeployMavenToTomcat</path>
          <!-- Set update to true to avoid exist war package can not be override error -->
          <update>true</update>
          <!-- Because deploy this maven project using plugin in pom so use the manager-script role user. -->
          <username>tomcat</username>
          <password>tomcat</password>
       </configuration>
    </plugin>
  3. Right click the maven project, click Run As —> Run Configurations menu item.
    maven project run as run configuration clean install tomcat7 deploy
  4. Input clean install tomcat7:deploy in the Goals input text box.
    maven project run as goals clean install tomcat7 deploy
  5. Click Run button, when you see BUILD SUCCESS in the output console, that means the maven project has been deployed to tomcat server successfully.
  6. Open a web browser, and input http://localhost:8080/manager/html, input admin / admin in the popup prompt. After you login, you can see DeployMavenToTomcat application in the application list.
    tomcat manager after deploy maven project
READ :   Deploy And Undeploy Java EE Web Applications In Tomcat

3. Pom.xml Full Content.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>DeployMavenProject</groupId>
  <artifactId>ToTomcat</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Deploy maven project to tomcat example.</name>
  <build>
    <finalName>Deploy maven project to tomcat example</finalName>
       <plugins>
          <!-- Below plugin will fix web.xml is missing and <failOnMissingWebXml> is set to true error. -->
          <plugin>
             <artifactId>maven-war-plugin</artifactId>
             <version>2.3</version>
             <configuration>
               <failOnMissingWebXml>false</failOnMissingWebXml>
             </configuration>
          </plugin>

          <!-- Use below plugin to deploy this maven project to tomcat server -->
          <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                  <url>http://localhost:8080/manager/text</url>
                  <path>/DeployMavenToTomcat</path>
                  <!-- Set update to ture to avoid exist war package can not be override error -->
                  <update>true</update>
                  <username>tomcat</username>
                  <password>tomcat</password>
                </configuration>
           </plugin>
       </plugins>
   </build>
</project>

Reference

  1. Manage Maven Project Using Eclipse
  2. How To Install Tomcat 9.0 Correctly On Your Machine

1 Comment


  1. thank you so much this was very helpful

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.