Maven : A simple webApp

In the last post we covered basic maven commands. Here we will create a simple web app. Lets get the project structure:

$> mvn archetype:generate -Dfilter=webapp-j2ee14

Notice that I am using a filter here as I dont want to get the whole list of archetype and then search it. The filter use the following format: [groupId:]artifactId. If you use single word without :, only artifactId will be checked. The filtering applied is a case sensitive contains on the artifactId (and groupId if set).



Heres the pom.xml:

<project xmlns="" xmlns:xsi=""





Notice, the packaging is ‘war‘ now. Also there are dependencies of servlet and jsp. Make a note of the scope of these dependencies (provided), we will get to it shortly. There is a ‘plugin‘ node. This is the compiler information. Here it tells maven that which version of java to be used for compilation. Here it says to use 1.4 version. If this node is not there, maven takes the default. For maven version 3 (that we are using), the default java version is 5. For an older version maven 2, the default version is 1.4.

To add on other dependencies, you can search in google for the correct groupid-artifactId-version that need to be added in pom.xml.

Now lets compile this, it will download the dependencies. Again make sure you are at the root of the project where your pom.xml is. After compile do a package, or you can directly call package

$>mvn package


Lets deploy this on tomcat.

Note- When you run Tomcat from Eclipse, it won’t show you the default page at localhost:8080/. Run it outside of Eclipse to see that page. Also, you should have a admin password. By default, Tomcat does not enable admin or manager access. To enable it, you have to edit the ‘%TOMCAT_FOLDER%/conf/tomcat-users.xml' manually. Like add following (tomcat 7) :

<role rolename="manager-gui"/>
 <role rolename="manager-script"/>
 <role rolename="manager-jmx"/>
 <role rolename="manager-status"/>
 <role rolename="admin-gui"/>
 <role rolename="admin-script"/>

 <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

Now deploy the war from admin console and test:


Lets automate this a bit.

The tomcat credential needs to be provided to maven for an automatic deployment. This is done in settings.xml (%mavenpath%\apache-maven-3.1.1\conf) on maven side. Add the following:


Note that ‘server’ node should be inside ‘servers’ node in setting.xml. We can directly add username/password in our pom.xml, byt lets stick to this best practice.

Add the following in your pom.xml:


Above is for tomcat 7. The url will change for previous version.

In context.xml (in your conf folder in tomcat) , though this is optional and is not related to maven3 deployment to tomcat7 ,sometimes jar locking might happen,so to be on the safer side.

<Context antiJARLocking="true" antiResourceLocking="true">


Now issue
$ mvn tomcat:deploy  //If you are deploying for the first time and that doesnt exsist
$mvn tomcat:redeploy //If the path already exsist

Remember to start tomcat before maven deployment. If deployment is success , your application will be available at http://localhost:8080/mywebapp/

Let us see now the scope node in dependencies which we left earlier.

Dependency scope includes dependencies as per the current stage of the build. Various Dependency Scopes are:

  • compile: This scope indicates that dependency is available in classpath of project. It is default scope.
  • provided: This scope indicates that dependency is to be provided by JDK or web-Server/Container at runtime.
  • runtime: This scope indicates that dependency is not required for compilation, but is required during execution.
  • test: This scope indicates that the dependency is only available for the test compilation and execution phases.
  • system: This scope indicates that you have to provide the system path.
  • import: This scope is only used when dependency is of type pom. This scope indicates that the specified POM should be replaced with the dependencies in that POM’s section.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: