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 make maven deploy to tomcat in eclipse project.

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 the Next button, check Create a simple project(skip archetype selection) 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.
    eclipe-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 maven war plugin XML code into pom.xml between project XML tag in pom.xml file.
    <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 the 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. Maven Project Deploy To Tomcat.

  1. Before we can deploy 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 deploy to tomcat 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 maven tomcat plugin 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 deploy maven project to tomcat.
    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 deploy to tomcat server process complete 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

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 maven tomcat 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>

4. How To Deploy Maven Project To Tomcat 8 With Maven Tomcat Plugin 7.

  1. If you want to use maven tomcat plugin version 7 to depploy mave project to tomcat version 8, follow below steps.
  2. Edit mave project pom.xml file maven tomcat plugin section as below.
    <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>tomcat8_vy</server>
        </configuration>
    </plugin>
  3. Add below xml content in maven settings.xml file ( located in maven configuration folder such as /apache-maven-3.6.3/conf/settings.xml ).
    <servers>
      <server>
        <id>tomcat8_vy</id>
        <username>tomcat_server_username</username>
        <password>tomcat_server_password</password>
      </server>
    </servers>
  4. Edit TOMCAT_HOME/conf/tomcat-users.xml file, add above tomcat_server_user account in
    <tomcat-users xmlns="http://tomcat.apache.org/xml"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
                  version="1.0">
      <role rolename="manager-script"/>
      <role rolename="manager-gui"/>
      <role rolename="manager-jmx"/>
      <role rolename="manager-status"/>
      <user username="tomcat_server_username" password="tomcat_server_password" roles="manager-script,manager-gui,manager-jmx,manager-status"/>
    </tomcat-users>
  5. Execute command mvn tomcat7:deploy or mvn tomcat7:redeploy in the web-app directory where pom.xml file is saved.

5. How To Deploy Maven Project To Embedded Tomcat With Special Port Number.

  1. If your tomcat listen on port number ( for exaple 9090 ) other than default 8080.
  2. You can deploy the maven project to the tomcat use command mvn -Dmaven.tomcat.port=9090 tomcat7:deploy.
  3. You can also edit maven project pom.xml file, add maven.tomcat.port properties for maven tomcat plugin configuration like below.
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <!-- deploy maven project to tomcat 6.x <artifactId>tomcat6-maven-plugin</artifactId> -->
        <version>2.2</version>
        <configuration>
         <maven.tomcat.port>9090</maven.tomcat.port>
         <!-- or <port>9090</port>-->
       </configuration>
    </plugin>

Reference

  1. Manage Maven Project Using Eclipse
  2. How To Install Tomcat 9.0 Correctly On Your Machine
  3. Run Mojo: run your Maven war project quickly!

2 thoughts on “How To Deploy A Maven Project To Tomcat From Eclipse”

  1. Hi, This is very helpful.

    I have tried this, I reached the end of the article with the application list but can you post the final URL to access the application DeployMavenToTomcat. Thanks in advance

Leave a Comment

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.