In a previous article, we have discussed how to create a 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 a web project or application using maven in Eclipse IDE.
Now, let's test this web project by adding simple Servlet and deploy on tomcat server.
To execute the about maven command in Eclipse IDE, Click on 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.
Now click on the Run and monitor the output in console.
That's all. I hope you understood the flow of creating a web project using Maven in Eclipse. Do comment if you need any help or issues.
Let me list out what are tools and technologies that I have used to create a 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 GroupId, ArtifactId, package and click on finish.
- GroupId: net.javaguides.maven-web-project
- Artifact Id: MavenWebApp
- package: net.javaguides.maven_web_project
Step-5
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 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 net.javaguides.mavenwebapp package and write the following code in it.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 installTo execute the about maven command in Eclipse IDE, Click on 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.
Now click on the Run and monitor the output in console.
Comments
Post a Comment
Leave Comment