How to create a web project using maven in eclipse

How to create a web project using maven in eclipse

In this post, we will show you how to create a dynamic web project using maven in Eclipse IDE.

Tools and technologies used -

  • Eclipse Mars.2 (4.5.2)
  • Maven 3.3.3
  • JavaSE 1.8
  • Servlet API 3.1.0
  • Apache Tomcat 7.0.47 (Embeded)

Let’s start step by step to create a web project using maven in eclipse.

 

Step #1

Go to New menu → Other..MavenMaven Project and click on Next.

Maven Web Project Project - BORAJI.COM

 

Step #2

In New Maven Project wizard, select Create a simple project and click on Next.

Maven Web Project Project - BORAJI.COM

 

Step #3

In next wizard, add project information as shown below and click on Finish.

Learn more about Maven naming conventions on groupid artifactId and version.

Maven Web Project Project - BORAJI.COM

 

Step #4

Open pom.xml file and add servlet dependency and Tomcat maven plugin in it as shown below.

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.boraji.tutorial.spring</groupId>
  <artifactId>maven-web-project-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <failOnMissingWebXml>false</failOnMissingWebXml>
  </properties>

  <dependencies>
    <!-- Servlet API -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

      <!-- Embedded Apache Tomcat required for testing war -->

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

After changes in pom.xml file, update the maven web project.

To update web maven project, right click on maven-web-project-example project → Maven → Update Project.

 

Step #5

Review the maven web project structure.

Maven Web Project Project - BORAJI.COM

 

Step #6

Now create a HelloWorldServlet class under com.boraji.tutorial.servlet package and write the following code in it.

HelloWorldServlet.java

package com.boraji.tutorial.servlet;
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;

/**
 * @author imssbora
 */
@WebServlet("/hello")
public class HelloWorldServlet 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 #7

Build the project using the following maven command.

mvn clean install

To execute the about maven command in Eclipse IDE, go to Run menu → Run Configuration.. to create a new configuration.

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

Maven Web Project Project - BORAJI.COM

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

Maven Web Project Project - BORAJI.COM

 

Step #8

Run embedded tomcat server using the following command.

mvn tomcat7:run

To execute the about maven command in Eclipse IDE, create a new configuration as shown in the below image.

Maven Web Project Project - BORAJI.COM

Now run configuration to start tomcat server. The console output will look like as shown in the below images.

Maven Web Project Project - BORAJI.COM

 

Step #9

Open browser and type http://localhost:8080/hello URL in address bar.

Maven Web Project Project - BORAJI.COM