The new Maven/Eclipse integration - Part 1

Creating a new Maven project in Eclipse

A new and greatly improved version of the Maven 2 Eclipse plugin has just come out. In a series of short articles, I'll be looking at what's new, and how to get the most out of this new version.

In this first installment, we will look at how to create a new Maven project from within Eclipse.

Installing the plugin

You can install the Maven Eclipse Plugin in the usual way, by using the Sonatype update site: http://m2eclipse.sonatype.org/update.

Creating a new Maven project

In this new version, the Maven project Wizard has been considerably improved. You can open this wizard by selection "New -> Other" and then opening the "Maven" entry. At this point, you get to choose from four options:

  • Check out a project from Subversion
  • Create a new Maven project
  • Create a new module
  • Create a new POM.XML
The first option lets you check an existing Maven project out of a Subversion repository. This option is a convenient way to import an existing project into your Eclipse environment, as the new Eclipse project will be configured for both Maven and Subversion. If you check out a parent project, the child modules will appear both as nested modules within the parent project, and as separate projects in your Eclipse workspace.

There are still a few bugs to be ironed out here when using multi-module projects, though. In the current version, for example, the main project that you check out will be correctly configured for Subversion and Maven, but things like JUnit will not work out-of-the-box - you have to configure a new JUnit run configuration for each sub-project. It would be nice if you could use "Run As -> JUnit Test" from the contextual menu in the same way as for any other project.

In the other projects created in the Eclipse environment, on the other hand Maven is configured correctly and you can run your unit tests (and other things) from the contextual menu as you would expect. However, in these secondary projects, Subversion is not configured! A simple "Team -> Share Project" does the trick, but even so, it would be nice if it was automatic.

You can also create a brand new Maven project, using a wizard to define the general project metadata and dependencies. You can select the archetype you want to use from a very complete list of available archetypes, including archetypes from Appfuse, Codehaus, Tapestry, Apache and others. This is very cool - I can never remember the syntax for the mvn:archetype create command.

The third option is to create a module as a child of a parent project. The last option is to create a POM.XML file for an existing project, and lets you enter some basic project metadata and specify the dependencies.

Overall, the m2eclipse team have done a nice job. Although there are still a few minor issues to iron out, it is very promising - you'll never have to write a pom.xml file or run the mvn:archetype command by hand again!

Until next time...

John Smart
Optimizing your software development process
http://www.wakaleo.com | john.smart@wakaleo.com

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Eugene Kuleshov replied on Thu, 2008/03/20 - 12:10am

I'd like to hear more details about "not working" Run As -> JUnit Test. Can you please open a jira issue and provide a test project that would allow us to reproduce this, because it is quite common scenario and it works out of the box on the projects I've tried. Same for the other issues that require ironing in your opinion. If we won't know what those issues are, it will be really hard for us to fix them. :-)

As for Subversion configuration, it doesn't really have anything todo with Maven integration. The issue is completely on Subclipse side, which explicitly disable sharing of the nested projects. The good news is that solution for this issue is in the works.

BTW, you have forgotten "Materialize Maven Project" option that allows to import projects into Eclipse workspace based on Maven metadata for the artifacts available from Maven repositories. I have blogged about that in more details at http://www.jroller.com/eu/entry/maven_project_materialization

Guillaume Bilodeau replied on Thu, 2008/03/20 - 9:44am

It's good to see improvement in this area. Maven/Eclipse integration has been poor at best until today, especially with regards to compile and runtime JAR dependencies - I always end up putting them in WEB-INF/lib anyway. Have you evaluated Q4E yet and if so, how does it stack up against the new m2eclipse?

Cheers,
GB 

gumnaam suri replied on Fri, 2008/03/21 - 6:46am

I can't seem to get the hierarchical layout work. When I check out the parent project from svn, it does check out the children projects but it doesn't populate the correct source structure etc. All I see is a bin directory in my children projects.

Eugene Kuleshov replied on Fri, 2008/03/21 - 11:23am in response to: gumnaam.sur

Assuming you have Maven metadata in the projects you checking out, you need to use "Checkout as Maven project", then all configuration will be done for you automatically. I would also recommend to review our demo script at http://docs.codehaus.org/display/M2ECLIPSE/Demo+Script

gumnaam suri replied on Fri, 2008/03/21 - 11:48am

What is maven metadata ? Are you referring to pom.xml ? I did indeed do "check out has Maven project". I'll check out the demo , thanks for links.

Werner Keil replied on Mon, 2008/03/24 - 7:26pm in response to: umnitsa

Interesting question.

Q4E is on the path towards a possible Eclipse Subproject.

 Similar to Subversion, where at least 2 major Eclipse plugins compete, this seems a bit like that here, too.

Any plans to cooperate in the Eclipse Foundation on this? 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.