How to deploy maven web app application to Tomcat

How to deploy maven web app application to Tomcat


Purpose – To setup a simplewebapp using maven and deploy to tomcat instance

Pre-requisites

Maven – 3.2.2
Tomcat – 7.0.32
JDK – 1.7.0_55

Procedure

Step1: Create a simple maven-archetype-webapp project using below command

mvn archetype:generate -Dfilter=maven-archtype-webapp

  <groupId>com.apache.middleware</groupId>
  <artifactId>simplewebapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>

Step2: Make sure that tomcat is listening on port 8080 and a user is setup for tomcat manager application access

File – <CATALINA_HOME>/conf/tomcat-users.xml

  <role rolename=”manager-gui”/>
  <role rolename=”manager-script”/>
  <user username=”tomcat” password=”tomcat” roles=”manager-gui,manager-script”/>

Verify that the below URL is accessisble with the user id that is configured in tomcat-users.xml file

http://localhost:8080/manager/html

Step3: Edit the Maven’s settings.xml file with the Tomcat server details as below

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

Step4: Edit the generated POM file with below tomcat plugin details

<plugins>
<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>TomcatServer</server>
<path>/simplewebapp</path>
</configuration>
</plugin>
</plugins>

Step5: Edit the global web.xml file located at <CATALINA_HOME>/conf/web.xml with below initialization parameter

<init-param>
   <param-name>readonly</param-name>
   <param-value>false</param-value>
</init-param>

This change will allow tomcat to accept HTTP put and delete requests which otherwise by default are rejected

Step6: Deploy the maven web application using below command

mvn clean tomcat7:deploy

Step7: Verify the application using below url

http://localhost:8080/simplewebapp/index.jsp

Response – Hello World!

Hope you enjoyed reading this article. Thank you.