Signup/Sign In

Creating Web Application using Maven Template

In the previous tutorials, we learnt about creating a simple java project by using the maven archetype and also understood how maven organizes the project structure based on the selected archetype. In this chapter we will learn about creating a web application project by using maven.

The easiest way to create a web project in maven is by using the maven archetype maven-archetype-webapp. Just open the command prompt and navigate to the folder where the project needs to be created. Run the below mentioned command, Maven will start executing the command and will create a complete project structure for a web based application.

mvn archetype:generate -DgroupId=com.sample.webproject -DartifactId=SampleWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Creating Simple Web Application in Maven

Below is the project structure for a web application generated by the maven by using the maven-archetype-webapp plugin.

Folder StructureDescription
SampleWebAppContains source folder and pom.xml file.
src/main/webappContains default index.jsp
src/main/webapp/WEB-INFContains web.xml file.
src/main/resourcesContains resource files used. (images or properties files)

Default Files Generated by using maven-archetype-webapp plugin

In any Java/J2ee web application, there will be always a file residing under the WEB-INF folder, web.xml. This is called as deployment descriptor file. This file is mainly used to configure the servlets for the following main factors:

  • to intercept the requests coming from the clients (browser end) and validating them before the requests hits the back end resources.
  • for manipulating the responses from the server side before they are directed to the client side.

Following is the pom.xml generated by the maven for the web application.

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

With this archetype maven generates a sample JSP file called index.jsp with the contents as shown below:

       <h2>Hello World!</h2>

Building and Deploying the Web App

Just open the command prompt and navigate to the root folder of the web app and issue the command mvn clean package. This command will compile, test and generates a war file of the project.

Clean and Build Web Project in Maven

Clean and Build Web Project in Maven

Once the war file is generated, copy the war file into the target folder of the Web Server(Any web server that you use) and start the server. You can test the application by invoking the web project's URL:


Clean and Build Web Project in Maven