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.
|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
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"
<name>SampleWebApp Maven Webapp</name>
With this archetype maven generates a sample JSP file called index.jsp with the contents as shown below:
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: