Maven Web Application: Creating web application in Maven 3

Learn to develop Maven based Web Application using maven build tool. In this tutorial you will learn how to create web application in Maven 3.

Maven Web Application: Creating web application in Maven 3

How to create Maven based Web Application? You will learn to create web application using maven archetype. We are using the Maven 3 build tool.

In this tutorial I will explain you the process and command of creating a web application using Maven 3 project management tool. You will also learn how to run it on the Tomcat Server. We will create the war file using

I am assuming you are aware of web applications and you have experience in the development of web applications. Here we will create a web application using Maven archetype.

Let's Start developing the application.

Step 1: Create sample web application using Maven archetype

Now we will tell you about the maven archetype for creating the web application. The maven-archetype-webapp is used to create web application using maven tool. Here is the syntax of the command:

mvn archetype:generate -DgroupId={package} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

You can replace the package and the project-name values and then create the project.

Here is the video instruction of creating web application in Mave: "How to make a web application with Maven?"

Here is simple example of creating web application:

mvn archetype:generate -DgroupId=net.roseindia -DartifactId=myfirstwebapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Here is the output of the command:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.


D:\>mkdir mavenexamples

D:\>cd mavenexamples

D:\mavenexamples>mvn archetype:generate -DgroupId=net.roseindia -DartifactId=myfirstwebapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] >>> maven-archetype-plugin:2.0-alpha-3:generate (default-cli) @ standalone-pom >>>
[INFO] <<< maven-archetype-plugin:2.0-alpha-3:generate (default-cli) @ standalone-pom <<<
[INFO] --- maven-archetype-plugin:2.0-alpha-3:generate (default-cli) @ standalone-pom ---
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] Generating project in Interactive mode
Define value for version:  1.0-SNAPSHOT: :
Confirm properties configuration:
groupId: net.roseindia
artifactId: myfirstwebapp
version: 1.0-SNAPSHOT
package: net.roseindia
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: net.roseindia
[INFO] Parameter: packageName, Value: net.roseindia
[INFO] Parameter: package, Value: net.roseindia
[INFO] Parameter: artifactId, Value: myfirstwebapp
[INFO] Parameter: basedir, Value: D:\mavenexamples
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: D:\mavenexamples\myfirstwebapp
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.411s
[INFO] Finished at: Tue Jan 14 11:07:49 IST 2014
[INFO] Final Memory: 7M/17M
[INFO] ------------------------------------------------------------------------

Step 2: Understanding Maven directory structure.

The above command will create a web application. Here is the directory structure of the web application:

  |- src

Step 3: The maven pom.xml file

The maven pom.xml file contains the information about project and its dependencies. Here is the code of the pom.xml file:

<project xmlns="" xmlns:xsi=""
  <name>myfirstwebapp Maven Webapp</name>

Step 4: Code of the web.xml file

Here is the code of the web.xml file:

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

  <display-name>Archetype Created Web Application</display-name>

Step 5: Code of index.jsp file.

Here is the code of the index.jsp file:

<h2>Hello World!</h2>

The index.jsp file simply displays the "Hello World!" message on browser.

Step 6: Building war file

First of go to the directory "myfirstwebapp". To create a war file you just have to type following command on the console:

mvn package

Here is the output of the program:

D:\mavenexamples\myfirstwebapp>mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building myfirstwebapp Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myfirstwebapp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ myfirstwebapp ---
[INFO] No sources to compile
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myfirstwebapp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\mavenexamples\myfirstwebapp\src\test\resources
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ myfirstwebapp ---
[INFO] No sources to compile
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myfirstwebapp ---
[INFO] No tests to run.
[INFO] --- maven-war-plugin:2.2:war (default-war) @ myfirstwebapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [myfirstwebapp] in [D:\mavenexamples\myfirstwebapp\target\myfirstwebapp]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\mavenexamples\myfirstwebapp\src\main\webapp]
[INFO] Webapp assembled in [27 msecs]
[INFO] Building war: D:\mavenexamples\myfirstwebapp\target\myfirstwebapp.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.421s
[INFO] Finished at: Tue Jan 14 12:09:53 IST 2014
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------

Step 7: Deploying application

Copy the war file into webapps directory of your Tomcat server and start the Tomcat. Type http://localhost:8080/myfirstwebapp in your browser. The "Hello World!" message should be displayed in the brower.

Check more tutorials of Maven 3