M2Eclipse and Web Tools
Posted by Mike Haller
on Friday, March 19. 2010
at 16:40
in Eclipse
M2Eclipse and Eclipse Web Tools don't play along nicely. I can't blame the Sonatype people for not having the resources or the focus on WTP Integration and that they cannot officially support it full-blown - it's complicated.But as many people develop for the Java Enterprise Edition platform and many people using Maven and many people use Eclipse WTP, i think we as a community should discuss possible solutions.
Here are my $.2
Assume you've got a standard webapp artifact which is built using Maven. Naturally, you have your applications's dependencies declared in Maven's project descriptor. Some of the dependencies are libraries you need (runtime libraries), some of them are for testing only and some of them are already available in the target platform, such as the Servlet APIs.
Maven accounts for this by using dependency scopes: Runtime, Compile, Test, Provided, System etc. Maven also uses these scopes to determine the correct classpath for testing and for packaging. Test dependencies for example are not packaged in the final web archive, so are provided libraries not packaged. Only what's needed at runtime is packaged and available within the .war file.
So, your pom.xml looks like this:

A webapp's pom.xml with multiple dependencies in different scopes
M2Eclipse's POM Editor nicely shows the dependencies including their scope. When you import this project to Eclipse and enable Dependency Management, M2Eclipse will add a Classpath Library to the Eclipse Project:

Maven Dependencies puts everything in one scope
As you can see, all the dependencies are in a single Classpath Library called Maven Dependencies: there's the Servlet APIs, there is JUnit and there is Spring with some of its transitive dependencies. Neither WTP nor M2Eclipse distinguishes the scopes, there is only one Classpath Library from an Eclipse's point of view.
When you publish and deploy this project to a WTP-managed JEE application container, you will end up having either none or all of those libraries in the WEB-INF/lib folder. Well, that's not entirely true, since it also depends on whether you have some of these dependencies as Eclipse Projects in your workspace and Workspace Resolution turned on, but that's a detail we can put aside for a moment and does not have an impact on my suggestion.
Now to the simple idea: suppose M2Eclipse would use multiple Classpath Libraries, one for each scope:

Separate Classpath Libraries for each Maven Dependency Scope
That way, even Eclipse and WTP could make use of Maven's built-in support for multiple scopes for dependencies and the developer could decide (or M2Eclipse could do it automatically) which Classpath Libraries and thus which dependencies are deployed to the application server, using the project's J2EE Module Dependencies property page:
The result is that only the runtime dependencies are deployed to the WEB-INF/lib folder, in this case Spring and its needed spring-asm and commons-logging libraries. Neither JUnit nor the Servlet APIs would be deployed.
I think this may help to solve one of the integration problems.
What do you think?
