While using Maven as build tool in our project, I found it very difficult to create a Dynamic Web Project which supports Maven dependencies and can execute in Eclipse! I have seen lot of people using Maven just as build tool and for local setup uses jar files in some /lib directory. I wanted to remove this dependencies on local /lib for jar files and resolve everything with Maven. My project should be a Dynamic web project with Maven dependencies enabled.
Here is a simple tutorial which you can go through to create Dynamic Web Project having Maven enabled in Eclipse. This project can be used as base project and can be easily converted to most kind of project like Struts based, Spring MVC based etc.
Required Tools
For this tutorial, I assume you have following setup in your machine.
1. JDK 1.5 or above (download)
2. Eclipse 3.2 or above (download)
3. Maven 2.0 or above (download)
4. M2Eclipse Plugin (download)
Step 1: Create Maven Project in Eclipse
Create a new project in Eclipse. Goto File > New > Project.. and select Maven Project from the list. Click Next.
![eclipse-maven-new-project eclipse-maven-new-project](http://img.viralpatel.net/2010/07/eclipse-maven-new-project.png)
Enter “MavenWeb” as Project name and click Next. On Configuration screen, select war in Packaging and also check the checkbox for src/main/webapp.
![new-maven-project new-maven-project](http://img.viralpatel.net/2010/07/new-maven-project.png)
Once done, click Finish. This will create a Maven project in Eclipse.
Step 2: Generate Eclipse Project with WTP
Let us convert the Maven project to Dynamic Web Project for Eclipse. For this we will use following maven command.
1 | mvn eclipse:eclipse -Dwtpversion=1.5 |
Goto the folder where the new project is created and execute above command.
C:\Workspace\Test\MavenWeb>mvn eclipse:eclipse -Dwtpversion=1.5
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - MavenWeb:MavenWeb:war:0.0.1-SNAPSHOT
[INFO] task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:eclipse]
[INFO] Adding support for WTP version 1.5.
[INFO] Using Eclipse Workspace: C:\Workspace\Test
[INFO] no substring wtp server match.
[INFO] Using as WTP server : Apache Tomcat v5.5
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "MavenWeb" to C:\Workspace\Test\MavenWeb.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Wed Jul 28 13:55:09 CEST 2010
[INFO] Final Memory: 7M/30M
[INFO] ------------------------------------------------------------------------
That’s it. We just created Dynamic Web Project from Maven project. Now refresh the project in Eclipse.
Step 3: Change Project Facet
The above step by default set the project facet to JDK 1.4. We need to modify this and set project facet to JDK 5. Right click on MavenWeb project and select Properties (shortcut: Alt+Enter). From the Properties dialog box, select Project Facets. Now click on “Modify Project…” button and change the project facet to Java 5.0
![project-facet-maven-eclipse project-facet-maven-eclipse](http://img.viralpatel.net/2010/07/project-facet-maven-eclipse.png)
Step 4: Setting Build Path
We need to specify the Maven jar dependencies in Java Build Path of our MavenWeb project. Open Properties.. dialog box (Alt+Enter) and select Java Build Path. Click “Add Library..” button and select “Maven Managed Dependencies” and click Finish.
![maven-eclipse-build-path maven-eclipse-build-path](http://img.viralpatel.net/2010/07/maven-eclipse-build-path.png)
Step 5: Hello World Servlet
Our dynamic web project with maven support is done now. Let us add a small Hello World servlet to this project and see how it works.
Open pom.xml from root folder and copy following content into it.
File: pom.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | < modelVersion >4.0.0</ modelVersion > |
04 | < groupId >MavenWeb</ groupId > |
05 | < artifactId >MavenWeb</ artifactId > |
06 | < packaging >war</ packaging > |
07 | < version >0.0.1-SNAPSHOT</ version > |
08 | < description ></ description > |
12 | < artifactId >maven-war-plugin</ artifactId > |
13 | < version >2.0</ version > |
19 | < groupId >javax.servlet</ groupId > |
20 | < artifactId >servlet-api</ artifactId > |
21 | < version >2.5</ version > |
Also create a servlet file HelloWorldServlet.java. We will add this servlet in net.viralpatel.maven package.
File: /src/main/java/net/viralpatel/maven/HelloWorldServlet.java
01 | package net.viralpatel.maven; |
03 | import java.io.IOException; |
04 | import java.io.PrintWriter; |
06 | import javax.servlet.ServletException; |
07 | import javax.servlet.http.HttpServlet; |
08 | import javax.servlet.http.HttpServletRequest; |
09 | import javax.servlet.http.HttpServletResponse; |
11 | public class HelloWorldServlet extends HttpServlet { |
13 | private static final long serialVersionUID = 1031422249396784970L; |
15 | public void doGet(HttpServletRequest req, HttpServletResponse resp) |
16 | throws ServletException, IOException { |
18 | resp.setContentType( "text/html" ); |
20 | PrintWriter out = resp.getWriter(); |
21 | out.print( "Hello World from Servlet" ); |
Once the servlet is created, let us configure this in web.xml. Note that in our maven project no web.xml is present. We will create one at /src/main/webapp/WEB-INF/ location.
File: /src/main/webapp/WEB-INF/web.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
03 | xmlns = "http://java.sun.com/xml/ns/javaee" |
04 | xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
05 | xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
06 | id = "WebApp_ID" version = "2.5" > |
07 | < display-name >HelloWorldServlet</ display-name > |
09 | < welcome-file >hello-world</ welcome-file > |
13 | < servlet-name >HelloWorldServlet</ servlet-name > |
15 | net.viralpatel.maven.HelloWorldServlet |
17 | < load-on-startup >1</ load-on-startup > |
20 | < servlet-name >HelloWorldServlet</ servlet-name > |
21 | < url-pattern >/hello-world</ url-pattern > |
That’s All Folks
Our dynamic web project with Maven support in Eclipse is completed. Run the web project in eclipse (Alt+Shirt+X, R)
![hello-world-maven-eclipse-web-project hello-world-maven-eclipse-web-project](http://img.viralpatel.net/2010/07/hello-world-maven-eclipse-web-project.png)