Friday, December 31, 2010

Maven, Eclipse and Tomcat

Following up on my previous post, I went through another frustrating experience trying to develop a webapp that was built using Maven and deployed into Tomcat through Eclipse.

The challenges I ran into were as follows:

  1. Tomcat 6.0 supports only Java 1.5 and Servlet 2.5. But, the Java and Dynmaic Web Module facets in Eclipse pick Java 1.6 and Servlet 3.0 by default.
  2. The Dynamic Web Module facet in Eclipse looks in a folder called WebContent for the web content whereas Maven maintains the web content in src/main/webapp.
  3. The Dynamic Web Module facet once applied to a project cannot be modified i.e, you cannot change from 3.0 to 2.5 after you discover that your Tomcat will not support Servlet 3.0.
  4. Eclipse does not provide a way to remove the Dynamic Web Module facet from your project.
Before I go into the steps for integrating a Maven webapp with Eclipse and Tomcat, let me first address the challenges of modifying or removing incorrect web facets.



How do I modify the Dynamic Web Module facet for my project ?

In Eclipse, if you try to remove the Dynamic Web Module facet, you will get an error saying "Dynamic Web Module 2.5 cannot be uninstalled". The only way to modify the facet is to first remove it and then reapply it. To learn how to remove it, read the next section.


How do I remove the Dynamic Web Module facet from my project ?

Step 1. Right-click on your project in Eclipse Package Explorer and select Close Project.
Step 2. Open a command-line terminal and go to your project folder.
Step 3. Open the .project file in a text editor and remove the following lines:
Step 4. Open the .classpath file and remove the following lines:
Step 5. Go into the .settings folder and delete the following files:
Step 6. Go back into Eclipse, right-click on your project and select Open Project Your project should now have all facets removed and should appear like a regular project.

What are the steps to integrate a Maven web archetype into Eclipse Tomcat ?

Step 1. I assume you already have a Maven web app in your Eclipse workspace. If not, here are some quick steps.

(i) In a command-line terminal, enter the following command:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

You can find out more about this command at Maven - Guide to Webapps

(ii) In Eclipse, select File -> Import ... -> Existing Maven Projects. You will be prompted to select the directory the project located on your filesystem. This should complete the Maven webapp creation in Eclipse.

Step 2. Select your project in Eclipse Package Explorer and select Properties.

Step 3. Select Project Facets in the Properties window and click on the "Convert to faceted form ..." link

Step 4. Select Java 1.5 from the list of facets.

Step 5. Select Dynamic Web Module 2.5 from the list of facets.

Step 6. Click on "Further configuration available..." link below the list of facets.

Step 7. In the Modify Faceted Project window that opens, change the value in the "Content directory" field to reflect maven's target folder.

Step 8. Click OK and OK.

Your project is now both a Maven project as well as an Eclipse Dynamic Web Module project.

If you want to deploy to the Tomcat instance that you setup in Eclipse, just right-click your project in Package Explorer and select Run -> Run on Server. You will be prompted to select your Tomcat instance and the application will be deployed.

If you need instructions on how to setup a Tomcat instance in Eclipse, refer my earlier post at Tomcat from within Eclipse.

If you want to deploy to an external Tomcat instance running outside of Eclipse, you can follow the instructions at How to create a Maven web app and deploy to Tomcat - fast

If you have any other questions, just drop a comment and I will get back to you.

Ciao!

5 comments:

Anonymous said...

Thanks for the help on removing the facets, allowed me to unravel a Web project back to a regular Java project.

bazil said...

Thanks, it will be useful migrating from Glassfish 3 to Apache Geronimo

Anonymous said...

Thanks mate. Very helpful.

Jose Fernandez said...

Thank you so much for this! I spent a few hours yesterday trying to accomplish the same thing with the Maven Eclipse plugin to no avail. Your instructions worked perfectly :)

Anonymous said...

Thanks a lot for you instructions to resolve this issue