Maven, Tomcat and Eclipse

Setting up a productive environment for your web programming experience is going to be a real timesaver once you get it set up correctly. And with Eclipse you can have that Tomcat container of yours up and running with automatic deployment of beans and jsp pages in a snap. Add Maven dependency management into the mix and you have yourself a good coctail to take your mind off those tedious deploy sequences, and instead concentrate on what you love most - hacking the code.

First of all, go download Eclipse Europa, specifically the Eclipse IDE for Java EE Developers distribution. Its about 125MB last time I checked. This has all the nice tools and plugins you'll need to effectively develop JEE applications.

Okay, you're all set, eh. Now you'll need to download Apache Tomcat. We'll be working with 5.5 here, but any other version should work fine as well. So go on, and fetch that last Core distribution. Unzip/untar it somewhere and we'll go on with setting it up to work with Eclipse.

Open up your server view (Window->Show View->Other...)

screenshot of show view

And we're ready to add a server. Right click in your server view "New->Server" and select a Tomcat 5.5 and click Next. Now select your tomcat installation directory (where you unpacked that zip/tar.gz file) and you're as you click Finish.

Setting up facets

As you may, or may not know, Eclipse relies on something it calls facets for adding certain behaviours to a project. If you want your project to be able to behave as a dynamic web project, you need your project to enable the WTP nature. If you want maven, then thats another chapter.

If you are creating a new Eclipse project as a Dynamic web project this is already set up for you. But if you are checking out an already existing project then you will need to add facets to your projects. So we'll do that. In your project directory you have a file named .project. Open that, and add a link to the facet nature org.eclipse.wst.common.project.facet.core.nature. It will look something like this:

<natures>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>

Now reload your project (F5). Right

click your project -> Properties and find the "Project Facets" editor. Here you select Dynamic Web Module and Java. And thats about it.Facets dialog

Maven dependencies

You're not still building your projects with ant are you? Setting up a Maven project is a bit out of the scope of this article, but I'll include some bits about setting up your Maven project in Eclipse.

You'll need to install the m2eclipse plugin. And when that is done, right click on your project and click "Maven->Enable Dependency Management". You will now notice that you will have an extra library source for your project.

Maven Library screenshot

But we're not quite ready yet. You might want bundle your dependencies for the project at deploy time. Meaning that the jar libraries you depend on will get copied over into the WEB-INF/lib folder. Right click your project, and select Properties again. Now go to the "J2EE Module Dependencies" and select what you want bundled.

Deploying your webapp

You are now ready to deploy your web application. Go to your servers view, right click your Tomcat instance and "Add or Remove Projects".
Select the projects you wish to deploy and you're ready to start developing.

Be Sociable, Share!

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

AddThis Social Bookmark Button

10 Responses to “Maven, Tomcat and Eclipse”

  1. Hi, very interesting, but it doesn’t work fine.
    When I make some change on my jsp or java, it’s need to restart tomcat. The tomcat status changes to ‘Restart’. This way, it’s no possible to have produtivity. Can you help me. I have a standard web maven project, eclipse ganymed and tomcat 6.

  2. May be it is me who is missing something: you have added Maven dependencies but i don’t see how Maven is being used here…

  3. …It is me again, i know you said:

    “Add Maven dependency management into the mix and you have yourself a good coctail to take your mind off those tedious deploy sequences”

    but the deployment (add + remove ) is not being handled by Maven but by WTP. Please correct me if i am wrong!

  4. The “J2EE Module Dependencies” part helped a lot.
    Thanks!

  5. The “J2EE Module Dependencies” part helped a lot.
    Thanks!!!!

    I lost 3 hours wondering how to do this

  6. Yes, the J2EE Module Dependencies was usefull.

    Is it possible to configure that with maven?

  7. The J2EE module dependencies is for deploying to an Tomcat instance you are running through Eclipse…

    What exactly are you trying to do?

  8. Yes, you are correct. Its with WTP. This is for development. If you want to do deployment with maven you could check out the Maven2 Cargo plugin.

  9. Its so that all the dependencies you add in your pom will become available in Eclipse. Meaning that you won’t have to have 2 sets of dependencies.

  10. Yeah. I worked through this problem by setting up some extra builders in Eclipse that would run ‘mvn war:exploded’ when I would save a file. It might work for you as well.