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
Below is the project structure for a web application generated by the maven by using the maven-archetype-webapp plugin.
Folder Structure | Description |
SampleWebApp | Contains source folder and pom.xml file. |
src/main/webapp | Contains default index.jsp |
src/main/webapp/WEB-INF | Contains web.xml file. |
src/main/resources | Contains 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="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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.webproject</groupId>
<artifactId>SampleWebApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SampleWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>SampleWebApp</finalName>
</build>
</project>
With this archetype maven generates a sample JSP file called index.jsp with the contents as shown below:
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
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.
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:
http:/localhost:8080/<projectname>/index.jsp