Guide to Create a Simple Maven Web Application

In this guide, we create a simple web application with the Maven Archetype plugin. We’ll run this web application in a Servlet container named jetty or tomcat, add some dependencies, write a simple Servlet, and generate a WAR file. At the end of this chapter, you will be able to start using Maven to accelerate the development of web applications.

You can also create Maven projects in Eclipse IDE. Check out below articles:
Table of contents
  1. Creating the Maven Web Project
  2. Maven Standard Directory Layout
  3. Adding J2EE Dependencies
  4. Adding a Simple Servlet
  5. Mapping the Simple Servlet in Deployment Descriptor(web.xml)
  6. Run Web Application
  7. Demo
  8. Conclusion

1. Creating the Maven Web Project

To create your web application:
mvn archetype:generate -DgroupId=com.companyname.simpleweb -DartifactId=simple-webapp -Dpackage=com.companyname.projectname -Dversion=1.0-SNAPSHOT
Note that whole command should be single line. After build success, we will see below output in command line console.
[INFO] Parameter: package, Value: com.javadevelopersguide
[INFO] Parameter: groupId, Value: com.javadevelopersguide
[INFO] Parameter: artifactId, Value: maven-webapp
[INFO] Parameter: packageName, Value: com.javadevelopersguide
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Ramesh_Study\maven\guides\maven-webapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 25.499 s
[INFO] Finished at: 2018-06-20T12:35:13+05:30
[INFO] ------------------------------------------------------------------------

2. Maven Standard Directory Layout

eclipse diagram here
Initial POM for the maven-webapp Project 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.javadevelopersguide</groupId>
  <artifactId>maven-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>maven-webapp 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>maven-webapp</finalName>
  </build>
</project>
you will need to configure the Maven Compiler plugin to target Java 8.
<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.javadevelopersguide</groupId>
 <artifactId>maven-webapp</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>maven-webapp Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>

  <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
   <scope>provided</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.0</version>
   <scope>provided</scope>
  </dependency>


  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <finalName>maven-webapp</finalName>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
  </plugins>
 </build>


</project>
Notice the packaging element contains the value war. This packaging type is what configures Maven to produce a web application archive in a WAR file.
It is a web application and it should created web.xml(deployment descriptor). Example: Contents of src/main/webapp/WEB-INF/web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>
Imports it into Eclipse IDE – File -> Import… -> Maven -> Existing Maven Projects into workspace.

3. Adding J2EE Dependencies

To write a servlet, we’ll need to add the Servlet API as a dependency to the project’s POM.
<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
 <scope>provided</scope>
</dependency>
It is also worth pointing out that we have used the provided scope for this dependency. This tells Maven that the JAR is "provided" by the container and thus should not be included in the WAR. If you were interested in writing a custom JSP tag for this simple web application, you would need to add a dependency on the JSP 2.0 API. Use the configuration shown in this example:
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>jsp-api</artifactId>
 <version>2.0</version>
 <scope>provided</scope>
</dependency>
  

4. Adding a Simple Servlet

Let’s add a simple servlet to this application and make some changes to the pom.xml and web.xml to support this change. create a package named 'com.javadevelopersguide.mavenwebapp.web' under this package create SimpleServlet.java.
package com.javadevelopersguide.mavenwebapp.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleServlet extends HttpServlet {
 
 private static final long serialVersionUID = 1L;

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  PrintWriter out = response.getWriter();
  out.println(" SimpleServlet Executed");
  out.flush();
  out.close();
 }
}
Our SimpleServlet class is just that: a servlet that prints a simple message to the response’s Writer. To add this servlet to your web application and map it to a request path, add the servlet and servlet-mapping elements shown in the following web.xml to your project’s web.xml file. The web.xml file can be found in src/main/webapp/WEB-INF.

5. Mapping the Simple Servlet in Deployment Descriptor(web.xml)

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Archetype Created Web Application</display-name>

 <servlet>
  <servlet-name>simple</servlet-name>
  <servlet-class>
   com.javadevelopersguide.mavenwebapp.web.SimpleServlet
  </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>simple</servlet-name>
  <url-pattern>/simple</url-pattern>
 </servlet-mapping>

</web-app>
That's it. Now you can run the web application either from eclipse or external tomcat or embedded server like the jetty.

6. Run Web Application

  1. If you have imported maven-webapp application into your eclipse then just do run as a server in tomcat server.
  2. You can also configure Jetty plugin. In this example let's configure Jetty plugin and run the web application.
Configuring the Jetty Plugin
<build>
 <finalName>maven-webapp</finalName>
 <plugins>
  <plugin>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.3</version>
   <configuration>
    <source>1.8</source>
    <target>1.8</target>
   </configuration>
  </plugin>

  <plugin>
   <groupId>org.mortbay.jetty</groupId>
   <artifactId>maven-jetty-plugin</artifactId>
  </plugin>

 </plugins>
</build>
Run mvn jetty:run from the simple-webapp/ project directory as follows:
mvn jetty:run
[INFO] Logging to org.slf4j.impl.SimpleLogger(org.mortbay.log) via org.mortbay.log.Slf4jLog
[INFO] Context path = /maven-webapp
[INFO] Tmp directory =  determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = C:\Ramesh_Study\maven\guides\maven-webapp\src\main\webapp\WEB-INF\web.xml
[INFO] Webapp directory = C:\Ramesh_Study\maven\guides\maven-webapp\src\main\webapp
[INFO] Starting jetty 6.1.26 ...
[INFO] jetty-6.1.26
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
[INFO] Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

7. Demo

Once the Jetty server is started just hit the below URL in a browser:
http://localhost:8080/maven-webapp/simple

8. Maven with Eclipse

9. Conclusion

In this example, we have learned how to create a simple maven based web application and we have run this web application in a Servlet container named jetty, added some j2ee dependencies, written a simple Servlet, and generate a WAR file.
The source code for this simple maven web project is available on Github.
Github Repository : Simple Maven Web Application

Comments