Maven Eclipse Plugin

So far, we worked with Maven CLI. In this chapter, we install Eclipse Maven Plugin and learn to manage Maven Project in Eclipse IDE.

Eclipse Maven Plugin, M2Eclipse or simply M2E, provides integration between Eclipse IDE and Maven. Among other things, it assists in the following aspects of Maven.

  • view contents of local and remote Maven repositories.

  • provides wizards to create single module project or multi module Maven Projects.

  • convert existing projects to Maven.

  • mange project POM through GUI Editor with features such as context assistance.

  • add and manage dependencies and plugins with search feature.

  • build projects with Maven Builds.

Install Maven Eclipse Plugin

M2Eclipse (M2E) Plugin provides tight integration for Maven into Eclipse IDE.

To install M2E, start Eclipse and go to HelpInstall New Software … and it displays the Available Softwares window. In Work with text box, enter M2E update site http://download.eclipse.org/technology/m2e/releases and click Add and then in Add Repository window click OK. Then proceed with installation of plugin.

Maven Eclipse Plugin - M2E Update site

For earlier versions of Eclipse such as Juno, the appropriate version of M2E is 1.4.x. Toggle Show only the latest version of software check-box to list the earlier versions of M2E.

 
 

Maven Repository View

Before start using M2E, we need to check whether the plugin has properly indexed the local repository. This is essential to activate the M2E’s search features while adding the plugins or dependencies to the project.

To update the index, open Maven Repositories View with WindowsShow ViewOthersMavenMaven Repositories.

Maven Eclipse Plugin - Repository View

In the Maven Repositories View, expand Local Repositories and right click on Local Repository and from the context menu, select Rebuild Index. Once index is updated, we can browse the repository and view the artifacts and plugins stored there.

It is also desirable to rebuild index of Maven Central Repository which exists under the Global Repositories. However, it takes much longer than local repository and requires net connection.

Create Maven Project

Maven Eclipse Plugin - Maven Project Wizard

M2E provides wizard to create Maven Project. We can use the wizard to create either a bare bone project (without source packages and files) or a project derived from Maven Archetypes.

Open Maven Project Wizard with FileNewOthersMavenMaven Project.

The wizard opens Select project name and location window. The check-box Create a simple project allows either to create a bare bone project or project based on Maven Archetype. Leave it unchecked to select Maven Archetype in the next step. And also, leave the location to default and click Next.

Maven Eclipse Plugin - Project Location

Next in the Select an Archetype window, we can use filter field and get list of matching archetypes. To create a quickstart app, enter quick as Filter and from the shown list, select maven-archetype-quickstart and click next.

Maven Eclipse Plugin - Project Wizard Archetype Selection

It opens Specify Archetype parameters window, where we enter the project Maven Coordinates and package name.

Maven Eclipse Plugin - Project Artifact Details

Finally click Finish to create the project based on quickstart archetype. The project structure is as shown in the screenshot.

Maven Eclipse Plugin - Project Structure

Create Multi Module Project

To create Maven multi module project, first we need to create a top level or parent project. Create the project as explained in the previous section, but with slight changes.

In Select project name and location window, check Create a Simple Project field so that project doesn’t contain any source packages and files. Next, while specify the project coordinates select packing type as pom.

To the parent project, we can add modules using Maven Module Wizard. Modules are nothing but regular Maven projects but with a parent. To add a module to a project, right click on the parent project and from the context menu select Add Module. Alternatively, we can use FileNewOthersMavenMaven Module to open the Maven Module Wizard.

In the New Maven Module window, enter the name for the module and specify whether project is simple or based on archetype. If based on archetype, in the next window, select the archetype. After that, we have to specify module coordinates and package name and click finish to create the module. This creates the module and its directories within the parent directory in the Eclipse workspace.

 
 

Add Maven to Project

With M2E, we can add Maven build system to any existing project in Eclipse workspace.

To start with, modify the directory structure of the project as required by the Maven. In case, we wish to retain the existing directory structure then we have to add directory configuration elements to pom.xml as explained in the Manage Maven Directories, once M2E creates the pom.xml,

To enable Maven nature, right click on the project and in the context menu, select ConfigureConvert To Maven Project. M2E Plugin enables the Maven nature and adds pom.xml to the project.

Likewise, we can use MavenDisable Maven Nature from the project context menu to convert a Maven project to a regular project.

The next section, explains other features of M2E Plugin which comes handy to manage and build Maven Projects in Eclipse IDE.