Deploy a maven webapp to Tomcat

Assuming you have already installed and started tomcat in one of the previous steps, it is time to deploy our simple java web application to tomcat. We just need to configure a couple things and call a maven plugin and we will be up and running in no time.

1. Configure a tomcat user.

Open the <tomcat>/conf/tomcat-users.xml file and enter the following. If there are already tomcat users defined in the xml file then just add the new role and user to the list.

<tomcat-users>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="admin" password="admin" roles="admin,manager"/>
</tomcat-users>

Now we can access the tomcat manager (http://localhost:8080 and click “Tomcat Manager”) with username admin and password admin.

We also now have the permission to deploy applications to Tomcat.

2. Configure maven to be able to login to the tomcat manager application.

Open <maven>/conf/settings.xml and enter the following to the <servers> list:

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

3. Modify the pom.xml file to include the tomcat-maven-plugin.

In Eclipse, open the pom.xml file and replace the existing <build> with the following:

<build>
    <finalName>SpringGreetings</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <configuration>
                     <server>mytomcatserver</server>
                     <path>/springgreetings</path>
                     <url>http://localhost:8080/manager</url>
                     <username>admin</username>
                     <password>admin</password>
            </configuration>
        </plugin>
    </plugins>
  </build>

What is this plugin? Maven will download this plugin code now, if you don’t already have it in your local repository. This plugin will compile and deploy the application to tomcat. You don’t need to do anything else!

4. Deploy the application to tomcat.

Assuming tomcat is already up and running (it needs to be running in order to deploy!) enter the following in TERMINAL:

$ cd SpringGreetings
$ mvn tomcat:deploy

Now you can open a web browser with the url http://localhost:8080/springgreetings/

You can also see the application deployed in the <tomcat>/webapps folder. There should be a folder inside the webapps directory called “springgreetings” and another file called “springgreetings.war”

5. Make a change in Eclipse, redeploy.

Open the file index.jsp in Eclipse and change “Hello World!” to “Welcome to Spring Greetings!” and then redeploy in TERMINAL:

$ mvn tomcat:redeploy
2 Comments
Anonymous wrote on 2013-04-04 at 07:30:36:
I got the error while deploying: [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (default-cli) on project tutorial: Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://10.0.0.220:8080/manager/deploy?path=%2Ftutorial&war= -> [Help 1] And the only one that helps was small fixing in pom.xml: instead of http://localhost:8080/manager I use http://localhost:8080/manager/text


baki wrote on 2013-03-23 at 15:37:53:
I had problems with the "pom.xml" configuration when using tomcat 7 to deploy the application. Now it's working for me using this: I hope it helps :) org.apache.tomcat.maven tomcat7-maven-plugin 2.0 mytomcatserver http://localhost:8080/manager/text /SpringGreetings


Post a Comment

(required):