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+ (if not check out How to install Java JDK).

– Make sure that you have installed Maven and configured an environment path ( if not check out How to install Maven)

1. Creating the Maven Web Project

To create your web application, use the below maven command (Our project name: maven-cli-simple-webapp):

mvn archetype:generate -DgroupId=com.favtuts.simpleweb -DartifactId=maven-cli-simple-webapp -Dpackage=com.favtuts.simpleweb -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

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] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /home/tuts.heomi.net/github/java-maven-tutorials
[INFO] Parameter: package, Value: com.favtuts.simpleweb
[INFO] Parameter: groupId, Value: com.favtuts.simpleweb
[INFO] Parameter: artifactId, Value: maven-cli-simple-webapp
[INFO] Parameter: packageName, Value: com.favtuts.simpleweb
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/tuts.heomi.net/github/java-maven-tutorials/maven-cli-simple-webapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.954 s
[INFO] Finished at: 2022-06-24T10:16:13+07:00
[INFO] ------------------------------------------------------------------------

2. Maven Standard Directory Layout

Go ahead and open the pom.xml file. Initial POM for the maven-cli-simple-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.favtuts.simpleweb</groupId>
  <artifactId>maven-cli-simple-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>maven-cli-simple-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-cli-simple-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.favtuts.simpleweb</groupId>
  <artifactId>maven-cli-simple-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>maven-cli-simple-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-cli-simple-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.favtuts.mavenwebapp.web‘, under this package create SimpleServlet class.

package com.favtuts.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.favtuts.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-cli-simple-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-cli-simple-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] Configuring Jetty for project: maven-cli-simple-webapp Maven Webapp
[INFO] Webapp source directory = /home/tuts.heomi.net/github/java-maven-tutorials/maven-cli-simple-webapp/src/main/webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = /home/tuts.heomi.net/github/java-maven-tutorials/maven-cli-simple-webapp/target/classes
[INFO] Logging to org.slf4j.impl.MavenSimpleLogger(org.mortbay.log) via org.mortbay.log.Slf4jLog
[INFO] Context path = /maven-cli-simple-webapp
[INFO] Tmp directory =  determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = /home/tuts.heomi.net/github/java-maven-tutorials/maven-cli-simple-webapp/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = /home/tuts.heomi.net/github/java-maven-tutorials/maven-cli-simple-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-cli-simple-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.

Download Source Code

$ git clone https://github.com/favtuts/java-maven-tutorials.git
$ cd maven-cli-simple-webapp
$ mvn jetty:run
Access: http://localhost:8080/maven-cli-simple-webapp/simple

References

  1. Maven – How to create a Java web application project
  2. How to Create a Web Project Using Maven in Eclipse

Leave a Reply

Your email address will not be published. Required fields are marked *