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

Latest releases of Eclipse comes with M2Eclipse (M2E) plugin preinstalled. To check whether M2E plugin is already installed, go to WindowPreferences and if M2E plugin is installed then dropdown entry named Maven will be on the left panel of Preferences window.

In case no Maven in Preferences, then install it as follows. To install M2E, go to HelpInstall New Software … and it displays the Available Softwares window. In Work with text box, enter M2E update site 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

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.