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.
- Click File —> New —> Others menu, select Maven Project in the popup wizard dialog.
- Click the Next button, check Create a simple project(skip archetype selection) checkbox in the next dialog.
- Input the spring project group id, artifact id and select packaging type with war in the next dialog.
- 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.
- 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>
- 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.
2. Maven Project Deploy To Tomcat.
- 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"/>
- 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>
- Right-click the maven project, click Run As —> Run Configurations menu item.
- Input clean install tomcat7:deploy in the Goals input text box deploy maven project to tomcat.
- Click Run button, when you see BUILD SUCCESS in the output console, that means the maven deploy to tomcat server process complete successfully.
- 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.
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.
- If you want to use maven tomcat plugin version 7 to depploy mave project to tomcat version 8, follow below steps.
- 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>
- 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>
- 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>
- Execute command
mvn tomcat7:deploy
ormvn 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.
- If your tomcat listen on port number ( for exaple 9090 ) other than default 8080.
- You can deploy the maven project to the tomcat use command
mvn -Dmaven.tomcat.port=9090 tomcat7:deploy
. - 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
- Manage Maven Project Using Eclipse
- How To Install Tomcat 9.0 Correctly On Your Machine
- Run Mojo: run your Maven war project quickly!
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
thank you so much this was very helpful