Maven - Web Application



This chapter teaches you how to manage a web based project using Maven. Here you will learn how to create/build/deploy and run a web application.

Create Web Application

To create a simple java web application, we will use maven-archetype-webapp plugin. So, let's open the command console, go to the D:\Projects\MVN directory and execute the following mvn command.

D:\Projects\MVN>mvn archetype:generate 
-DgroupId=com.companyname.automobile 
-DartifactId=trucks
-DarchetypeArtifactId=maven-archetype-webapp 
-DinteractiveMode=false

Maven will start processing and will create the complete web based java application project structure as follows −

D:\Projects\MVN>mvn archetype:generate -DgroupId=com.companyname.automobile -DartifactId=trucks -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> archetype:3.4.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< archetype:3.4.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- archetype:3.4.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (3.9 kB at 8.2 kB/s)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: D:\Projects\MVN
[INFO] Parameter: package, Value: com.companyname.automobile
[INFO] Parameter: groupId, Value: com.companyname.automobile
[INFO] Parameter: artifactId, Value: trucks
[INFO] Parameter: packageName, Value: com.companyname.automobile
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\Projects\MVN\trucks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.926 s
[INFO] Finished at: 2025-09-15T16:47:51+05:30
[INFO] ------------------------------------------------------------------------
D:\Projects\MVN>

Now go to D:\Projects\MVN directory. You'll see a java application project created, named trucks (as specified in artifactId) as specified in the following snapshot. The following directory structure is generally used for web applications −

Java web application project structure

Maven uses a standard directory layout. Using the above example, we can understand the following key concepts −

Sr.No. Folder Structure & Description
1

trucks

contains src folder and pom.xml.

2

src/main/webapp

contains index.jsp and WEB-INF folder.

3

src/main/webapp/WEB-INF

contains web.xml

4

src/main/resources

it contains images/properties files.

POM.xml

<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.companyname.automobile</groupId>
   <artifactId>trucks</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>trucks 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>trucks</finalName>
   </build>
</project>

If you observe, you will find that Maven also created a sample JSP Source file.

Open src > main > webapp > folder to see index.jsp with the following code −

<html>
   <body>
      <h2>Hello World!</h2>
   </body>
</html>

Build Web Application

Let's open the command console, go to the D:\Projects\MVN\trucks directory and execute the following mvn command.

D:\Projects\MVN\trucks>mvn clean package

Maven will start building the project.

D:\Projects\MVN\trucks>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< com.companyname.automobile:trucks >------------------
[INFO] Building trucks Maven Webapp 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ trucks ---
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ trucks ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource from src\main\resources to target\classes
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ trucks ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ trucks ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Projects\MVN\trucks\src\test\resources
[INFO]
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ trucks ---
[INFO] No sources to compile
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ trucks ---
[INFO] No tests to run.
[INFO]
[INFO] --- war:3.4.0:war (default-war) @ trucks ---
[INFO] Packaging webapp
[INFO] Assembling webapp [trucks] in [D:\Projects\MVN\trucks\target\trucks]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\Projects\MVN\trucks\src\main\webapp]
[INFO] Building war: D:\Projects\MVN\trucks\target\trucks.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.035 s
[INFO] Finished at: 2025-09-15T16:55:02+05:30
[INFO] ------------------------------------------------------------------------

Deploy Web Application

Now copy the trucks.war created in D:\ > Projects > MVN > trucks > target > folder to your webserver webapp directory and restart the webserver.

Test Web Application

Run the web-application using URL: http://<server-name>:<port-number>/trucks/index.jsp.

Verify the output.

web page
Advertisements