Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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.

Step-4

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 tomcat server.

Step 6

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

<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>net.javaguides.maven-web-project</groupId>
    <artifactId>MavenWebApp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>MavenWebApp 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>
        <!-- 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>
    </dependencies>
    <build>
        <finalName>MavenWebApp</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


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
 */
@WebServlet("/hello")
public class SimpleServlet extends HttpServlet {

   private static final long serialVersionUID = 1L;

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
         throws ServletException, IOException {
      resp.setContentType("text/plain");
      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!

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

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

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}