27 May 2015

Introduction

In this example, I am going to show you how to build and deploy an web application in Tomcat7 using maven. If you are new to maven setup then check my another blog on maven settings.
tomcat7-maven-plugin plugin helps to deploy the application into tomcat. In this case we need to execute tomcat7:deploy

Required Software

  • JDK 1.7
  • Maven 3.3.x
  • Eclipse for J2EE
  • Tomcat 7

Steps to write code and deploy in Tomcat Server

  • Create a dynamic web project (Fill information in the below screens as shown below. Other screens will have no change)

  • Create a simple java project with src/main/java, src/main/resources as the source directory. Once project is created, you can add source directory from the below screen (Right click on project -> properties)

  • Convert the project into maven project (Right click on the project -> Configure -> Convert to Maven project)
  • Create the below index.jsp file as shown in the below image. Once this application is deployed and executed, This project has been build and deployed in Tomcat server by MAVEN message will be displayed

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello world</title>
</head>
<body>
 <h1>This project has been build and deployed in Tomcat server by MAVEN</h1>
</body>
</html>
  • Right click on the project -> Run As -> Run on server. Select tomcat7 to deploy. Then Next->Finish.
  • Once tomcat is started then hit http://localhost:8080/WebAppMavenBuild_Deploy/ URL and if you get the below screen then you are done with the project setup and your project is running perfectly fine.
  • Now the same build and deployment will be done using maven build.
  • Add the following piece of configuration in c:\user<user_name>.m2\settings.xml

<server>
	<id>TomcatServer</id>
	<username>root</username>
	<password>root</password>
</server>
  • Add the following lines in your tomcat_path\conf\tomcat-users.xml file

<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<user username="root" password="root" roles="manager-script,manager-gui"/>
  • Add the following lines in your pom.xml

..........
<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<version>2.0</version>
	<configuration>
		<url>http://localhost:8080/manager/text</url> <!-- Do not change -->
		<server>TomcatServer</server> <!-- Also configured in maven settings.xml file -->
		<path>/WebAppMavenBuild_Deploy</path>
		<update>true</update>   <!-- To redeploy the war file -->
		<username>root</username>
		<password>root</password>
	</configuration>
</plugin>

.............

<dependency>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<version>2.2</version>
	<type>maven-plugin</type>
</dependency>
  • Start the tomcat server
  • Right click on the project -> Run as -> Run Configurations… then follow the below screen to do maven build and deployment

  • You will see the successful build with the below message
Uploaded: http://localhost:8080/manager/text/deploy?path=%2WebAppMavenBuild_Deploy (26846 KB at 28169.2 KB/sec)
 
[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /WebAppMavenBuild_Deploy
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
  • Once build is successful then hit http://localhost:8080/WebAppMavenBuild_Deploy/ URL and if you get the below screen then you are done with the project deployment with maven and your project is running perfectly fine.


blog comments powered by Disqus
J2EE,SOAP,RESTful,SVN,PMD,SONAR,JaCoCo,HTTP,API,MAVEN,AngularJS,GitHub,LDAP,AOP,ORM,JMS,MVC,AWS,SQL,PHP,H2DB,JDBC