Maven - SPLessons

Maven Web Application

Home > Lesson > Chapter 15
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Maven Web Application

Maven Web Application

shape Introduction

In previous tutorials, projects were developed using maven archetypes, plug-ins, etc. Now, with templates, Maven Web Application can be created using the Webapp plug-in. The present chapter explains about webapp plug-in and how to create a sample Maven Web Application.

Webapp Plugin

shape Description

Normally, J2EE applications have WEB-INF folder, which consists of web.xml. web.xml files are called as Deployment Descriptor files that are used for servlets configurations as web.xml helps in: Maven Archetype Webapp plug-in creates the default file structure for the application.

shape Step-1

Open the command prompt and create a folder where the project has to be saved. Now, copy the below command in command prompt under the created folder as shown.
mvn archetype:generate -DgroupId=com.sample.webproject -DartifactId=WebApplication -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
By creating the required files for building an application, following output appears. By this, Maven also creates a web application structure with the application name WebApplication. The project structure in eclipse is as follows:

shape Step-2

Check for pom.xml and index.jsp file that has the following codes by default. pom.xml [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.sample.webproject</groupId> <artifactId>WebApplication</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>WebApplication 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>WebApplication</finalName> </build> </project> [/xml] index.jsp [c] <html> <body> <h2>Hello World!</h2> </body> </html> [/c]

Building WebApp

shape Step-3

Again open the command prompt, go to the root folder, enter the mvn clean package command and click enter to compile, test and create the WAR file for the project. The following output appears.

shape Step-4

One can view the WebApplication.war in target folder. Now, deploy that war file into the server. The following output appears that shows that the application has been created successfully.

Summary

shape Key Points

  • Webapp plug-in is required to generate the web applications in Maven.
  • Web application is build using the mvn clean package command.

shape Programming Tips

Make sure to deploy the generated war file into the server target folder.