A Developer's Diary

May 19, 2012

A simple java web application in minutes

You can set up your simple java web application in no time using maven. The application can be downloaded from maven's site.

Maven comes up with a single line command to create Hello World web application for you. The value to the argument archetypeArtifactId identifies that the project to be created is of type web application. In case you are running this command for the first time, it will take some time for the maven to download the dependent plugins first and then create the sample application

Create Web Application Project
mvn archetype:create -DgroupId=com.simple.web -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-webapp
Your sample application is created and ready to be packaged and deployed

Build the web application distributable
mvn package

Download Apache Tomcat
Make sure the version of apache tomcat you are downloading matches with the tomcat maven plugin. In my application, I am using version 6.0.29 for both the tomcat and the tomcat maven-plugin

Start Apache Tomcat
Go to Apache Tomcat bin directory and execute startup.sh or startup.bat script

Configure pom.xml in your web application
Configure pom.xml in your web application to use tomcat-maven-plugin
Add the following lines of code in your application's pom.xml file within the buildtag to enable tomcat-maven-plugin. The url is the tomcat manager's url which is used for deploying and undeploying web applications. This may be different in different tomcat versions. Check tomcat manager's documentation for the respective version.
<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/maven-v4_0_0.xsd">
  <name>webapp Maven Webapp</name>

Deploy Web Application
mvn tomcat:deploy
Open http://localhost:8080/webapp/index.jsp in your browser and you should be able to see the Hello World! message

Undeploy Web Application
mvn tomcat:undeploy

Redeploy Web Application
mvn tomcat:redeploy

List applications running on Apache Tomcat
mvn tomcat:list

Start Web Application
mvn tomcat:start

Stop Web Application
mvn tomcat:stop

No comments :

Post a Comment