HOW-TO: Create Java Web Application with Eclipse WTP and m2eclipse

by Rafał Borowiec on 15th June 2010

On daily basis we use Eclipse as a primary IDE for developing Java applications. We also are tightly coupled with Apache Maven as our main project management tool. A well known plugin that integrates Apache Maven into Eclipse is m2eclipse. One of its main features is the integration with Web Tools Project (WTP). In this short tutorial I will present how to start and configure Java Web Application using m2eclipse .

Requirements

m2eclipse

m2eclipse plugin may be installed via http://m2eclipse.sonatype.org/sites/m2e update site (Help > Install New Software). It is also required to install (at least) WTP support for m2eclipse from extras update site: http://m2eclipse.sonatype.org/sites/m2e-extras:
m2eclipse extras update site

m2eclipse settings

If you plan to use Subversion or CVS also install SCM support and proper SCM handler. Now let’s change the default Maven installation for m2eclipse from embedded to our fresh Maven installation (Preferences > Maven > Installation):
m2eclipse settings

m2eclipse settings

Server

The sample application will run on Tomcat 6 within Eclipse. To make it possible, define the new Tomcat 6 server. Select New > Other > Server. Click Next. Choose Tomcat 6 from Apache group and click Next. Finish:
Tomcat 6 server

Tomcat 6 server

Create Web Application

We are ready now to create a new Web Application. Select New Project Wizard and choose Maven Project from the list. In Archetype selection panel select maven-archetype-webapp as shown below:
Archetype selection

Archetype selection

Finish the project creation by filling archetype parameters:
Archetype parameters

Archetype parameters

Once project is created, navigate to JEE perspective to see its default layout:
Project layout

Project layout

Running the application

Right click on the application and select Run On Server. From the server list select Tomcat v6.0 server created earlier and click finish. That’s it. You can build up your application with your favorite Eclipse without losing any of its great features. And you still benefit from Maven.

Download

Sample Eclipse project: wtp-m2eclipse-demo
  • http://pulse.yahoo.com/_WW2SRXIJGCOQBQI2FGGJYTT6IU Hansctb Ctb

    Nice explanation, but in the final step I do not have the run On Server option, when following these steps. I do have it (Run As, Run On Server) when not using Maven, using default Eclipse Dynamic Web Project…

  • rborowiec

    Did you install all required plugins? m2extras too? If so, try updating maven configuration for the project.

    Recently I started new project (or two) with Eclipse 3.6 and m2eclipse selecting other archetype: webapp-javaee6. And it works perfectly. Please try this one. My team works with this project with no compaints.

  • http://www.facebook.com/people/Chaouachi-Nizar/1201005128 Chaouachi Nizar

    Good tuto, it helped me..I have a question, why we should change the default Maven installation for m2eclipse from embedded to our fresh Maven?

  • rborowiec

    It is not required, to be honest. But Nexus we use is configured to support maven2, so we always stick to maven2 version. Other reason is, I sometimes use command line to build the projects, not eclipse, and then it's easier to have the same installation. Third reason – maven2 is stable.

  • http://www.facebook.com/people/Jeff-Walker/1640501497 Jeff Walker

    I followed these directions and got a hello world jsp to deploy, but when I added a call to java, it couldn't find the class. The java class was in the same project. Any ideas?

  • rborowiec

    I have noticed this behaviour in Eclipse. You need to build the project. Or the best – clean and re-built the project, so all classes are compiled and accessible by Eclipse.

  • Nikita Tovstoles

    Thanks for a very helpful tutorial, Rafal. One question: after I completed the steps above (with Eclipse 3.6, jdk 1.6) I ended up with a project whose properties were set as such:
    -Java compiler set to 1.4
    -project facets: Dynamic Web Module v2.3

    I was able to set the earlier to 1.6 but attempting to update the latter resulted in “unable to set DWM to 2.5″. I ended up manually editing web.xml to 2.5 spec. Is that your experience as well?

    thanks
    -nikita

  • http://pulse.yahoo.com/_DCUAFNMCTUFDM6ZNPMVL4XMRII barbie

    ok

  • Anonymous

    As it goes to Java compiler the only thing is to add a maven compiler plugin and set source and target to 1.6 and update project configuraition with the plugin.

    As it goes to the I edited the web.xml too.

    To be more effective use other archetype to create a web project: webapp-javaee6. It works better with couple modifications.

  • zuxiong lin

    And now how to create  ,    
    m2e – Maven Integration for Eclipse 1.0.100.20110804-1717 org.eclipse.m2e.feature.feature.group Eclipse.org – m2e

    Eclipse Java EE IDE for Web Developers.Version: Indigo Service Release 1Build id: 20110916-0149

  • Pingback: Skype and Agile software development were really hot on our blog this year

  • nicolas ext

    Is there a way to add maven dependencies to the tomcat’s classpath (user entry) ?
    My depedencies are correctly exported to the web-inf/lib directory but my tomcat need some of them inthe classpath in order to start. 

  • Pingback: Maven with Eclipse | László GRAD-GYENGE