Sunday, October 2, 2011

How to enable Maven for Dynamic Web Projects in Eclipse

I'm currently toying around a bit with JSF 2.0 and came to the situation where I wanted to enable Maven dependency management. It's doable but unfortunately not as straightforward as it could be. That's why I'm outlining the steps that have to be done to do just that here. My environment is Eclipse 3.7 (Indigo) with Web Tools Platform (WTP) and m2e (Maven integration for Eclipse).

When creating a new Dynamic Web Project there is already the opportunity to create the Maven typical source folders. For a Web project these are at least
  • src/main/java
    Unsurprisingly this directory contains the Java source code for the Web application (Servlets, JSF components, Managed Beans, validators, renderers, etc.).
  • src/main/webapp
    This directory contains everything that normally resides in a directory called WebContent, i.e. JSPs, Facelets and the like as well as the special directories META-INF and WEB-INF.
Other standard Maven source folders such as src/main/resources may be created as well. Just remove the default source folder src and create the above source folders.

If the project already exists, this is just as easy. Create the correct source folders, move all content that already exists to the correct locations and delete the old source folders.

Now enable Maven by right-clicking the project in the Project Explorer and selecting Configure - "Convert to Maven Project" (This is for the new m2e plugin. For the old m2eclipse plugin this is Maven - "Enable Dependency Management"). Enter all details as required and remember to make sure the packaging type to war.

A few problems have to fixed first: Since the default compiler level for Maven projects is Java 5, Eclipse might complain that no such runtime could be found (if you use Java 5 then I guess you're clear of course). So first thing to do is add the plugin maven-compiler-plugin and configure Java 6 (or maybe 7). Right click the project, select Maven - "Add Plugin", search for maven-compiler-plugin and add it. In the pom you have to add the configuration manually to receive the following (code with Blogger is a pita so I didn't bother):


Then add other dependencies as you need.

Finally the so called Deployment Assembly has to be fixed. Basically this is a mapping from resources as they are in the project to resources as they appear once the application is deployed.

Right-click the project and go to the Properties. Open Deployment Assembly. There is still a mapping from /WebContent. This is not needed, so remove it. Create a new mapping from src/main/java to WEB-INF/classes by clicking "Add…" - Folder, select the desired folder and Finish. It should automatically get mapped to WEB-INF/classes. If not, correct that. Add another folder mapping from src/main/webapp to / (the root). Last but not least add a Java Build Path Entries mapping for Maven Dependencies. It should automatically get mapped to WEB-INF/classes. Again, if that's not the case, correct it. Now add all other mappings as you need them, this is just the bare minimum. In the end you should get something like that:


Now publish and verify. Everything should work.

1 comment: