Thursday, 29 July 2010

Maven Web Projects and Eclipse

Apart from dependency management, perhaps the biggest benefit I get from using Maven is being able to distribute sample code to people from my training courses and not worry too much about which IDE they are using. Because Maven separates the build process from the IDE, it makes it reasonably straightforward to open Maven projects created in other IDEs.

NetBeans is my IDE of choice and Maven support is bundled with the IDE from 6.7 onwards. There is no setup to do in this case (apart from perhaps pointing Netbeans at a local installation of Maven rather than the built-in version). Simply open the project and Netbeans will identify it as a Maven project by the presence of a pom.xml file.

With Eclipse, Maven support is not included in the base IDE. Other offerings, such as the excellent SpringSource Tool Suite, do include it. Mainly For the benefit of people on whom I inflict my demo projects and examples, here is a quick run-down on getting started with Maven and Eclipse.

Setting up Eclipse for Maven Projects

I'm assuming that you have installed your base plugins for whatever type of Java development you intend to do with Maven: the Web Tools Project (WTP) plugins if you are going to do some web development for example.

To add the required plugins to Eclipse, you need to go to and add the following update sites:


The names don't matter of course. "M2Eclipse Plugin" and "M2Eclipse Plugin Extras" are as good as any others.

For the M2Eclipse Plugin, there is only one component to select. Make sure you accept any licence agreements and certificates during the installation. This will give you basic Maven support for building Java (non-Web) projects. It's a common mistake to either forget or not be aware of the "extras" plugin but if you try and open a web project you'll end up with a complete mess in the Project Explorer because Eclipse doesn't understand the Maven web project layout.

For the M2Eclipse Plugin Extras, there are more choices and you probably don't need them all. In fact the only ones I tend to use are the Maven Integration for WTP and Maven SCM Integration. The latter is only required if you want to check out Maven projects from a version control system like Subversion (in which case you'll need the relevant Eclipse plugins to support that directly, as well as the Maven SCM Integration).

And that should be it. Open up the Maven project and Eclipse should show that it has recognised it with an "M" in the top left corder of the project icon in the Project Explorer.

In Eclipse, Maven goals are invoked using the Run menu or toolbar icon. To clean the project for example, right click on the project node, select "Run As", then "Maven Clean". To build the project and deploy the resulting JAR or WAR file to your local Maven repository, select "Run As", "Maven Install". To run a web application on an Eclipse-controlled application server, just choose "Run As", "Run on server" as usual.

No comments: