How to Create a Web Project Using Maven in Eclipse

DZone 's Guide to

How to Create a Web Project Using Maven in Eclipse

Creating a project in Maven is made easy with Eclipse!

· Java Zone ·
Free Resource

In a previous article, we have discussed how to create simple maven project in eclipse. In this article, we will show you how to create a web project or application using maven in Eclipse IDE.

Let me list out what are tools and technologies that I have used to create web project or application using maven in Eclipse IDE.

Tools and Technologies Used

  • IDE - Eclipse Neon
  • JDK - 1.8
  • Maven - 3.5.3
  • Apache Tomcat - 8.5
  • Servlet API - 3.1.0

Let’s start step by step to create a web project using Maven in Eclipse IDE.

Step 1

  • Open Eclipse
  • Click on File -> New -> Maven Project

Step 2

  • Don't select a simple project as shown in below diagram
  • Select default Workspace location
  • Click on Next

Step 3

Select the Maven archetype as maven-archetype-webapp and click on next.


Provide the GroupId,ArtifactId, package and click on finish.

  • GroupId: net.javaguides.maven-web-project
  • Artifact Id: MavenWebApp
  • package: net.javaguides.maven_web_project

Step 5

And you are all set. You should see a new Project in Eclipse with the following structure.

Now, let's test this web project by adding simple Servlet and deploy on a Tomcat server.

Step 6

Open pom.xml file and add servlet API dependency and the Maven compiler plugin as:

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <name>MavenWebApp Maven Webapp</name>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->

Step 7

Now. create a SimpleServlet class under the net.javaguides.mavenwebapp package and write the following code:

package net.javaguides.mavenwebapp;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * Servlet implementation class SimpleServlet
public class SimpleServlet extends HttpServlet {

   private static final long serialVersionUID = 1L;

   protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
         throws ServletException, IOException {
      resp.getWriter().write("Hello World! Maven Web Project Example.");

Step 8

Let's build the project using the following Maven command — mvn clean install.

To execute this Maven command in Eclipse IDE, click on the Run menu → Run Configuration to create a new configuration.

In the Run Configuration Wizard, double-click on Maven Build and provide the configuration information (Name, Base directory, and Goals) as shown in the below image.

Now, click on the Run and monitor the output in the console.

Step 9

Next, let's deploy our web project into Tomcat. First, we need to configure the Tomcat server in Eclipse IDE via the server section. Once deployment is a success, then open a browser and type the http://localhost:8080/hello URL in the address bar.

That's it! I hope you understood the flow of creating a web project using Maven in Eclipse. Let me know if you have any further questions in the comments section below!

java ,maven ,web ,eclipse ,ide ,how-to ,tutorial

Published at DZone with permission of Ramesh Fadatare . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}