Wednesday, January 9, 2013

Create a Dynamic Web Project with Maven in Eclipse Juno

Recently, I began to show my interest to start using Apache Maven in nearly all of my projects. Today, I was  trying to get hold of creating a Dynamic Web Project (Jave EE) project using the M2Eclipse Plugin, which is a fascinating tool to work with, but is tricky as well. While there are other related posts like here, they are outdated and in case you will follow them, you will be exhausted.

1. Prerequisites

You have to have the followings in advance:

  • Eclipse Juno for Java EE developers
  • JDK 1.6 or above
  • Apache Maven 3.0 or above
  • M2E Plugin (You can get this easily by searching it in the search bar of Eclipse Market Place inside the Help  menu of your Eclipse)
  • Apache Tomcat 

2. Create a Maven Project

2.1 Plugin Prerequisites 

You need to have a plugin called M2Eclipse which you can fetch it via Eclipse Marketplace (In case you do not know, just browse to Help and then you can find it there)

Some of you may not know but you also need another plugin called Eclipse WTP plugin. The latter is the plugin that lets you run Web Applications with Built-in servers. Have a look at the plugins you need to have  in advance:

Also note that there is another Maven Eclipse WTP plugin which is not depreciated in favor of the one you can see in the above picture. Everytime you need to install a plugin, you also need to restart your Eclipse.

2.2 Create your project

This step is just simple and easy. First, click on File, then go to New and click on Other... . In there you can search for the word "Maven" and you will get the following options:

Then, simple click on the Next button and in the next dialogue, select maven-archetype-webapp move on:

And the final step is to name your project:

You should by now see the following structure of your project with the new look:

Notice that you have your Deployment Descriptor which enables you to run it on Eclipse's internal server. Open your pom.xml and change it to the following:

  agent Maven Webapp

As you can see I just added the servlet-api.jar to the dependencies of the project, when later on we will have to compile our Maven project. Now, I will add a basic Servlet in the package src/main/java/servlet/

package servlet;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {
 private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException {
  PrintWriter out = response.getWriter();
  out.println("The app is up and running!");


After that, add the servlet information and pattern in the web.xml which you may find under src/main/webapp:

  Archetype Created Web Application

Now it is the time to compile the project. Here you have two options. First is to Right click on your project, then choose Maven and Click on Maven Clean and after that on Maven Install or else, run the following command inside your project directory:

cd /project/directory
mvn clean install

You can have your .war file in your target directory now and you can deploy it in your Apache Tomcat container, however the missing part here is that Eclipse does not know that your project is a web project, therefore you cannot deploy it in your local Apache Tomcat Server (Also your development Server). Every time you code, you need to compile and see the result in your Tomcat. Now we do two more steps so that we can have both the .war file and you will be able to run the application while coding in your tomcat server. First, go to Servers menu which resides in Window and then Show View:

And simply add a Apache Tomcat 7 instance, without changing any settings and click Finish.

3. Run the Project

Now, click on your project in Eclipse's navigation bar and click Refresh. As you can see, the project structure changes. Now you can simple right click on and run it on Server, as following:

And that is it! You now have a fully working Dynamic Web Project Maven Project in eclipse!

Remark: In contrast with a lot of tutorials out there, you should not manually change Project Facets of your project, while you are working with Maven projects in Eclipse. The project should read everything from your pom file and change the facets on the fly.

1 comment:

  1. Hi,To help the readers discover the entire website, provide a sitemap. For large collections of documents in Web Design Cochin which may be listed over several or many pages, provide a view all link, too.Thanks..........