Create a Simple Maven Web Application using Command Line

In this tutorial, 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.

Note that we are creating a Maven web application from the command line using Maven.

You can also create Maven projects in Eclipse IDE. Check out the below articles:

Pre-requisites 

- Make sure that you have installed Java 8+.
- Make sure that you have installed Maven and configured an environment path ( if not check out How to install Maven on Windows 10)

Table of contents

  1. Creating the Maven Web Project
  2. Maven Standard Directory Layout
  3. Import Maven Project in Eclipse IDE
  4. Adding J2EE Dependencies
  5. Adding a Simple Servlet
  6. Mapping the Simple Servlet in Deployment Descriptor(web.xml)
  7. Run Web Application
  8. Demo
  9. Conclusion

1. Creating the Maven Web Project

To create your web application, use the below maven command (Our project name: simple-webapp):
mvn archetype:generate -DgroupId=com.companyname.simpleweb -DartifactId=simple-webapp -Dpackage=com.companyname.projectname -Dversion=1.0-SNAPSHOT
Note that the whole command should be a single line. After build success, we will see the below output in the 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

Go ahead and open the pom.xml file. Initial POM for the maven-webapp project:
<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>
Now, let's configure our maven project to use the maven-compiler-plugin to compile the source code using Java 8.
Open the pom.xml file and add the below maven-compiler-plugin:
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
Here is the complete pom.xml file:
<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>
  <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 create web.xml(deployment descriptor). 
So open the src/main/webapp/WEB-INF/web.xml file to see its content:
<!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>

3. Import Maven Web Application in Eclipse IDE

Imports it into Eclipse IDE:
File -> Import… -> Maven -> Existing Maven Projects into workspace.

4. 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>
  

5. 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. 
Let's create a package named 'com.javadevelopersguide.mavenwebapp.web', under this package create SimpleServlet class.
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. 

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

Open the src/main/webapp/WEB-INF/web.xml file and add the following code to map servlet:
<!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.

7. Run Web Application

  1. If you have imported the maven-webapp application into your eclipse then just do run as a server in tomcat server.
  2. You can also configure the Jetty plugin. In this example let's configure the 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>
Go to the project directory and run the following command to run this Maven project in the Jetty server:
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

8. Demo

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

9. Conclusion

In this tutorial, 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.

The source code for this simple maven web project is available on Github.

Github Repository: Simple Maven Web Application

Comments

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

    ReplyDelete
  2. The maven archetype is missing, so you cannot even finish step 1.

    ReplyDelete

Post a Comment