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

June 15, 2010 | by Rafał Borowiec

View Comments

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

  • 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.
  • 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.
  • 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.
blog comments powered by Disqus