Chapter 14. Maven and Eclipse: m2eclipse

14.1. Introduction

Favicon

The Eclipse IDE is the most widely used IDE for Java development today. Eclipse has a huge amount of plugins (see http://www.eclipseplugincentral.com/) and an innumerable amount of organizations developing their own software on top of it. Quite simply, Eclipse is ubiquitous. The m2Eclipse project, provides support for Maven within the Eclipse IDE, and, in this chapter, we will explore the features it provides to help you use Maven with Eclipse.

14.2. m2eclipse

Favicon

The m2Eclipse plugins (http://m2eclipse.codehaus.org/) provide Maven integration for Eclipse. m2Eclipse also has hooks into the features of both the Subclipse plugin (http://subclipse.tigris.org/) and the Mylyn plugin (http://www.eclipse.org/mylyn/). The Subclipse plugin provides the m2eclipse plugin with the ability to interact with Subversion repositories, and the Mylyn plugin provides the m2eclipse plugin with the ability to interact with a task-focused interface that can keep track of development context. Just a few of the features m2eclipse provides include:

  • Creating and importing Maven projects

  • Dependency management and integration with the Eclipse classpath

  • Automatic dependency downloads and updates

  • Artifact Javadoc and source resolution

  • Creating projects with Maven Archetypes

  • Browsing and searching remote Maven repositories

  • POM management with automatic update to dependency list

  • Materializing a project from a Maven POM

  • Checking out a Maven project from several SCM repositories

  • Adapting nested multi-module Maven projects to the Eclipse IDE

  • Integration with Web Tools Project (WTP)

  • Integration with AspectJ Development Tools (AJDT)

  • Integration with Subclipse

  • Integration with Mylyn

  • Form-based POM Editor

  • Graphical Display of Dependency Graph

  • GUI Presentation of Dependency Tree and Resolved Dependencies

There are many more features in m2eclipse beyond the list above and this chapter introduces some of the more impressive features that are currently available. Let’s get started by installing the m2Eclipse plugin.

14.3. Installing the m2eclipse Plugin

Favicon

To install the m2Eclipse plugin, you will need to install some prerequisites. You will need to be running Eclipse 3.2 or higher, JDK 1.4 or higher, and you will also need to make sure that Eclipse is running on a JDK and not a JRE. Once you have Eclipse and a compatible JDK, you will need to install two Eclipse plugins: Subclipse and Mylyn.

14.3.1. Installing Prerequisites

Favicon

You can install these prerequisites when you install m2eclipse, just add a new remote update site to Eclipse for each of the prerequisite components. To install these prerequisites, go to HelpSoftware UpdatesFind and Install.... Selecting this menu item will load the Install/Update dialog box. Choose the "Search for new features to install" option and click Next. You will then be presented with a list of "Update sites to visit". Click New Remote Site..., and add a new update site for each new prerequisite. Add a new remote site for each plugin and then make sure that the remote site is selected. After you click Finish, Eclipse will then ask you to select plugins components to install. Select the components you want to install and Eclipse will download, install, and configure your plugins.

Note that if you are using a recent build of Eclipse 3.4 (Ganymede) your plugin installation experience may be slightly different. In Ganymede, you will select HelpSoftware Updates... which will load the "Software Updates and Add-ons" dialog. In this dialog, choose the Available Software panel and click on Add Site... which will load the simple "Add Site" dialog. Enter the URL of the update site you wish to add and click OK. In the "Software Updates and Add-ons" dialog, the available plugins from an update site will appear as soon as the site is added. You can then select the modules you want to install and click the Install... button. Eclipse will then resolve all the dependencies for the selected plugins, and ask you to agree to the plugin license. After Eclipse installs new plugins, it will likely ask you for permission to restart.

14.3.1.1. Installing Subclipse

Favicon

To install Subclipse, use the Eclipse plugin update site listed below.

For other versions of Subclipse, and for more information about the Subclipse plugin, please see the Subclipse project's web site at http://subclipse.tigris.org/.

14.3.1.2. Installing Mylyn

Favicon

To install JIRA integration with Mylyn, add the Mylyn extras Eclipse update URL, you'll want to do this if your organization uses Atlassian's JIRA for issue tracking. To install Mylyn use the following update sites:

For more information about the Mylyn project, see the Mylyn project's web site at http://www.eclipse.org/mylyn/.

14.3.1.3. Installing AspectJ Development Tools (AJDT)

Favicon

If you are installing the 0.9.4 release of m2eclipse, you may also want to install both the Web Tools Platform (WTP) and the AspectJ Development Tools (AJDT). To install the AJDT use one of the following update URLs in Eclipse:

For more information about the AJDT project, see the AJDT project's web site at http://www.eclipse.org/ajdt/.

14.3.1.4. Installing the Web Tools Platform (WTP)

Favicon

To install the Web Tools Platform (WTP). Use one of the following update URLs in Eclipse, or just look for the Web Tools Project in the Discovery Site which should already be in your Eclipse remote update sites list.

For more information about the Web Tools Platform, see the Web Tools Platform project's web site at http://www.eclipse.org/webtools/.

14.3.2. Installing m2eclipse

Favicon

Once you've installed the prerequisites, you can install the m2eclipse plugin from the following Eclipse update URL:

If you would like to install the latest snapshot development version of the plugin, you should use the update-dev URL instead of the previous URL:

To install m2eclipse, just add a the appropriate update site for m2eclipse. Go to HelpSoftware UpdatesFind and Install.... Selecting this menu item will load the Install/Update dialog box. Choose the "Search for new features to install" option and click Next. You will then be presented with a list of "Update sites to visit". Click New Remote Site..., and add a new update site for m2eclipse. Add a new remote site for m2eclipse and then make sure that the remote site is selected. After you click Finish, Eclipse will then ask you to select plugins components to install. Select the components you want to install and Eclipse will download, install, and configure m2eclipse.

If you've installed the plugin successfully, you should see a Maven option in the list of preferences options when you go to WindowPreferences....

14.4. Enabling the Maven Console

Favicon

Before we begin to examine the features of m2eclipse, let’s first enable the Maven console. Open the Console View by going to WindowShow ViewConsole. Then click on the little arrow on the right-hand side of the Open Console icon and select Maven Console as shown below:

Enabling the Maven Console in Eclipse

Figure 14.1. Enabling the Maven Console in Eclipse


Maven Console shows the Maven output that normally appears on the console when running Maven from the command line. It is useful to be able to see what Maven is doing and to work with Maven debug output to diagnose issues.

14.5. Creating a Maven Project

Favicon

When using Maven, project creation takes place through the use of a Maven archetype. In Eclipse, project creation takes place via the new project wizard. The new project wizard inside of Eclipse offers a plethora of templates for creating new projects. The m2eclipse plugin improves upon this wizard to provide the following additional capabilities:

  • Checking out a Maven project from a SCM repository

  • Creating a Maven project using a Maven archetype

  • Creating a Maven POM file

As shown in Figure 14.2, “Creating a New Project with m2eclipse Wizards”, all three of these options are important to developers using Maven. Let’s take a look at each one.

Creating a New Project with m2eclipse Wizards

Figure 14.2. Creating a New Project with m2eclipse Wizards


14.5.1. Checking Out a Maven Project from SCM

Favicon

m2eclipse provides the ability to check out a project directly from a SCM repository. Simply enter the SCM information for a project and it will check it out for you to a location of your choice as shown in Figure 14.3, “Checkout a New Project from Subversion”:

Checkout a New Project from Subversion

Figure 14.3. Checkout a New Project from Subversion


There are additional options in this dialog for specifying a particular revision by browsing the revisions in a Subversion repository or by simply entering the revision number manually. These features reuse of some of the features in the Subclipse plugin to interact with the Subversion repository. In addition to Subversion, the m2eclipse plugin also supports the following SCM providers:

  • Bazaar

  • Clearcase

  • CVS

  • git

  • hg

  • Perforce

  • Starteam

  • Subversion

  • Synergy

  • Visual SourceSafe

14.5.2. Creating a Maven Project from a Maven Archetype

Favicon

m2eclipse offers the ability to create a Maven project using a Maven Archetype. There are many Maven Archetypes provided in the list that comes with m2eclipse as shown in Figure 14.4, “Creating a New Project with a Maven Archetype”.

Creating a New Project with a Maven Archetype

Figure 14.4. Creating a New Project with a Maven Archetype


The list of archetypes in Figure 14.4, “Creating a New Project with a Maven Archetype” is a list generated by something called the Nexus Indexer. Nexus is a repository manager which is introduced in Chapter 16, Repository Manager. The Nexus indexer is a file which contains an index of the entire Maven repository, and m2eclipse uses it to list all of the available archetypes in the entire Maven repository. When this chapter was last updated, m2eclipse had approximately ninety archetypes in this Archetype dialog. Highlights of this list include:

[4]And these were just the archetypes that were listed under the Nexus Indexer Catalog, if you switch Catalogs you'll see other archetypes. While your results may vary, the following additional archetypes were available in the Internal Catalog:

  • Atlassian Confluence Plugin Archetype under com.atlassian.maven.archetypes

  • Apache Struts Archetypes under org.apache.struts

  • Apache Shale Archetypes under org.apache.shale

A catalog is simply a reference to a repository index. You can manage the set of catalogs that the m2eclipse plugin knows about by clicking on the Configure... button next to the catalog drop down. If you have your own archetypes to add to this list, you can click on Add Archetype....

Once you choose an archetype, Maven will retrieve the appropriate artifact from the Maven repository and create a new Eclipse project with the selected archetype.

14.5.3. Creating a Maven Module

Favicon

m2eclipse provides the ability to create a Maven module. Creating a Maven module is almost identical to creating a Maven project as it also creates a new Maven project using a Maven archetype. However, a Maven module is a subproject of another Maven project typically known as a parent project.

Creating a New Maven Module

Figure 14.5. Creating a New Maven Module


When creating a new Maven module you must select a parent project that already exists inside of Eclipse. Clicking the browse button displays a list of projects that already exist as shown in Figure 14.6, “Selecting a Parent Project for a New Maven Module”:

Selecting a Parent Project for a New Maven Module

Figure 14.6. Selecting a Parent Project for a New Maven Module


After selecting a parent project from the list, you are returned to the New Maven Module window and the Parent Project field is populated as shown in Figure 14.5, “Creating a New Maven Module”. Clicking Next will then display the standard list of archetypes from Section 14.5.2, “Creating a Maven Project from a Maven Archetype” so you can choose which one should be used to create the Maven module.

14.6. Create a Maven POM File

Favicon

Another important feature m2eclipse offers is the ability to create a new Maven POM file. m2eclipse provides a wizard to easily create a new POM file inside of a project that is already in Eclipse. This POM creation wizard is shown in Figure 14.7, “Creating a New POM”:

Creating a New POM

Figure 14.7. Creating a New POM


Creating a new Maven POM is just a matter of selecting a project, entering the Group Id, Artifact Id, Version, choosing the Packaging type, and providing a Name into the fields provided and m2eclipse. Click the Next button to start adding dependencies.

Adding Dependencies to a New POM

Figure 14.8. Adding Dependencies to a New POM


As you can see in Figure 14.8, “Adding Dependencies to a New POM” here are no dependencies in the POM yet. Just click the Add button to query the central Maven repository for dependencies as shown next in Figure 14.9, “Querying the Central Repository for Dependencies”:

Querying the Central Repository for Dependencies

Figure 14.9. Querying the Central Repository for Dependencies


Querying for dependencies is as easy as entering the groupId for the artifact you need. Figure 14.9, “Querying the Central Repository for Dependencies” shows a query for org.apache.commons with commons-vfs expanded to see which versions are available. Highlighting the 1.1-SNAPSHOT version of commons-vfs and clicking OK takes you back to the dependency selection where you can either query for more artifacts or just click finish to create the POM. When you search for dependencies, m2eclipse is making use of the same Nexus repository index that is used in the Nexus Repository Manager from Chapter 16, Repository Manager.

Now that the you’ve seen the m2eclipse features for creating a new project, let’s look at a similar set of features for importing projects into Eclipse.

14.7. Importing Maven Projects

Favicon

m2eclipse provides three options for importing a Maven project into Eclipse including:

  • Import an existing Maven project

  • Check out a Maven project from SCM

  • Materialize a Maven project

Figure 14.10, “Importing a Maven Project” shows the wizard for importing projects with the options for Maven provided by m2eclipse:

Importing a Maven Project

Figure 14.10. Importing a Maven Project


The dialog in Figure 14.10, “Importing a Maven Project” is displayed by using the FileImport command in Eclipse and then filtering the options by entering the word maven in the filter field. As noted above, there are three options available for importing a Maven project into Eclipse including: Maven Projects, Check out Maven Project from Subversion, and Materialize Maven Projects.

Importing a Maven project from Subversion is identical to the creation of a Maven project from Subversion as discussed in the previous section so discussion of it would be redundant. Let’s move on now to review the other two options for importing a Maven project into Eclipse.

14.7.1. Importing a Maven Project

Favicon

m2eclipse can import a Maven project with an existing pom.xml. By pointing at the directory where a Maven project is located, m2eclipse detects all the Maven POMs in the project and provides a hierarchical list of them as shown in Figure 14.11, “Importing a Multi-module Maven Project”.

Importing a Multi-module Maven Project

Figure 14.11. Importing a Multi-module Maven Project


Figure 14.11, “Importing a Multi-module Maven Project” displays the view of the project being imported. Notice that all the POMs from the project are listed in a hierarchy. This allows you to easily select which POMs (and therefore which projects) that you want to be imported into Eclipse. Once you select the project you would like to import, m2eclipse will import and build the project(s) using Maven.

14.7.2. Materializing a Maven Project

Favicon

m2eclipse also offers the ability to "materialize" a Maven project. Materialization is similar to the process of checking out a Maven project from Subversion, but instead of manually entering the URL to the project’s Subversion repository, the Subversion URL is discovered from the project’s root POM file. You can use this feature to "materialize" projects from nothing more than a POM file if the POM file has the appropriate elements to specify the location of a source repository. Using this feature, you can browse the central Maven repository for projects, and materialize them into Eclipse projects. This comes in handy if your project depends on a third-party open source library, and you need to get your hands on the source code. Instead of tracking down the project web site and figuring out how to check it out of Subversion, just use the m2eclipse project to magically "materialize" the Eclipse project.

Figure 14.12, “Materializing a Maven Project” shows the wizard after choosing to materialize Maven projects:

Materializing a Maven Project

Figure 14.12. Materializing a Maven Project


Notice that the dialog box for Maven artifacts in Figure 14.12, “Materializing a Maven Project” is empty. This is because no projects have been added yet. In order to add a project, you must click the Add button on the right side and select a dependency to add from the central Maven repository. Figure 14.13, “Selecting Artifact to Materialize” shows how to add a project:

Selecting Artifact to Materialize

Figure 14.13. Selecting Artifact to Materialize


Upon entering a query, candidate dependencies will be located in the local Maven repository. After a few seconds of indexing the local Maven repository, the list of candidate dependencies appears. Select the dependency to add and click OK so that they are added to the list as shown in Figure 14.14, “Materializing Apache Camel”.

Materializing Apache Camel

Figure 14.14. Materializing Apache Camel


Upon adding a dependency, you have the option of telling the m2eclipse plugin to check out all projects for the artifact.

14.8. Running Maven Builds

Favicon

m2eclipse modified the Run As... and Debug As... menus to allow you to run a Maven build within Eclipse. Figure 14.15, “Running an Eclipse build with Run As..” shows the Run As... menu for an m2eclipse project. From this menu you can run one of the more common lifecycle phases like clean, install, or package. You can also load up the Run configuration dialog window and configure a Maven build with parameters and more options.

Running an Eclipse build with Run As..

Figure 14.15. Running an Eclipse build with Run As..


If you need to configure a Maven build with more options, you can choose Run Configurations... and create a new Maven build. Figure 14.16, “Configuring a Maven Build as a Run Configuration” shows the Run dialog for configuring a Maven build.

Configuring a Maven Build as a Run Configuration

Figure 14.16. Configuring a Maven Build as a Run Configuration


The Run configuration dialog allows you to specify multiple goals and profiles, it exposes options like "skip tests" and "update snapshots", and allows you to customize everything from the project to the JRE to the environment variable. You can use this dialog to support any custom Maven build that you wish to launch with m2eclipse.

14.9. Working with Maven Projects

Favicon

The m2eclipse plugin also provides a set of features for working with Maven projects once they are inside of Eclipse. There are many features that ease the ability to use Maven in Eclipse so let’s dive right into them. In the previous section, I materialized a Maven project and selected a subproject from the Apache Camel project named camel-core. We’ll use that project to demonstrate these features.

By right-clicking on the camel-core project, and selecting the Maven menu item, you can see the available Maven features. Figure 14.17, “Available Maven Features” shows a screenshot of this:

Available Maven Features

Figure 14.17. Available Maven Features


Notice in Figure 14.17, “Available Maven Features” the available Maven features for the camel-core project, including:

  • Adding dependencies and plugins

  • Updating dependencies, snapshots and source folders

  • Creating a Maven module

  • Downloading the source

  • Opening Project URLs such as the Project Web Page, Issue Tracker, Source Control, and Continuous Integration tool.

  • Enabling /Disabling workspace resolution, nested Maven modules and dependency management

These features are also big time savers so let’s review them briefly.

14.9.1. Adding and Updating Dependencies and Plugins

Favicon

Let’s say we’d like to add a dependency or a plugin to the camel-core POM. For the sake of demonstration, we're going to add commons-lang as a dependency. (Please note that the functionality for adding a dependency or a plugin is exactly the same so we’ll demonstrate it by adding a dependency.)

m2eclipse offers two options for adding dependencies to a project. The first option is by manually editing the POM file to type in the XML to add the dependency. The downside to manually editing the POM file to add a dependency is that you must already know the information about the artifact, or use the features discussed in the next section to manually locate the artifact information in the repository indexes. The upside is that after manually adding the dependency and saving the POM, the project’s Maven Dependencies container will be automatically updated to include the new dependency. Figure 14.18, “Manually Adding a Dependency to the Project's POM” shows how I added a dependency for commons-lang to the camel-console POM and the Maven Dependencies container was automatically updated to included it:

Manually Adding a Dependency to the Project's POM

Figure 14.18. Manually Adding a Dependency to the Project's POM


Manually adding a dependency works well but requires more work than the second approach. Upon manually adding the dependency element to the POM, the Eclipse progress in the lower right-hand corner of the Eclipse workbench reflects the action as shown in Figure 14.19, “Updating Maven Dependencies”:

Updating Maven Dependencies

Figure 14.19. Updating Maven Dependencies


The second option for adding a dependency is much easier because you don’t have to know any information about about the artifact other than its groupId. Figure 14.20, “Searching for a Dependency” shows this functionality:

Searching for a Dependency

Figure 14.20. Searching for a Dependency


By simply entering a groupId into the query field, m2eclipse queries the repository indexes and even shows a version of the artifact that is currently in my local Maven repository. This option is preferred because it is such a tremendous time saver. With m2eclipse, you no longer need to hunt through the central Maven repository for an artifact version.

14.9.2. Creating a Maven Module

Favicon

m2eclipse makes it very easy to create a series of nested projects in a multi-module Maven project. If you have a parent project, and you want to add a module to the project, just right click on the project, go the Maven menu, and choose "New Maven Module Project". m2eclipse will walk you through the project creation process to create a new project, then it will update the parent project's POM to include the module reference. Before m2eclipse came along it was very difficult to use a hierarchy of Maven projects within Eclipse. With m2eclipse, the details of the underlying relationships between parent and child projects are integrated into the development environment.

14.9.3. Downloading Source

Favicon

If the central Maven repository contains a source artifact for a particular project, you can download the source from the repository and expose it to the Eclipse environment. When you are trying to debug a complex issue in Eclipse, nothing can be easier than being able to right click on a third-party dependency and drill into the code in the Eclipse debugger. Select this option, and m2eclipse will attempt to download the source artifact from the Maven repository. If it is unable to retrieve this source artifact, you should ask the maintainers of the project in question to upload the appropriate Maven source bundle to the central Maven repository.

14.9.4. Opening Project Pages

Favicon

A Maven POM contains some valuable URLs which a developer may need to consult. These are the project's web page, the URL for the source code repository, a URL for a continuous integration system like Hudson, and a URL for an issue tracker. If these URLs are present in a project's POM, m2eclipse will open these project pages in a browser.

14.9.5. Resolving Dependencies

Favicon

You can configure a project to resolve dependencies from a workspace. This has the effect of altering the way that Maven locates dependency artifacts. If a project is configured to resolve dependencies from the workspace, these artifacts do not need to be present in your local repository. Assume that project-a and project-b are both in the same Eclipse workspace, and that project-a depends on project-b. If workspace resolution is disabled, the m2eclipse Maven build for project-a will only succeed if project-b's artifact is present in the local repository. If workspace resolution is enabled, m2eclipse will resolve the dependency via the eclipse workspace. In other words, when workspace resolution is enabled, project's don't have to be installed in the local repository to relate to one another.

You can also disable dependency management. This has the effect of telling m2eclipse to stop trying to manage your project's classpath, and it will remove the Maven Dependencies classpath container from your project. If you do this, you are essentially on your own when it comes to managing your project's classpath.

14.10. Working with Maven Repositories

Favicon

m2eclipse also provides some tools to make working with Maven repositories a bit easier. These tools provide functionality for:

  • Searching for Artifacts

  • Searching for Java classes

  • Indexing Maven repositories

14.10.1. Searching For Maven Artifacts and Java classes

Favicon

m2eclipse adds a couple of items to the Eclipse Navigation menu that make searching for Maven Artifacts and Java classes easy work. Each option is available by clicking on the Navigate menu as shown in Figure 14.21, “Searching for Artifacts and Classes”:

Searching for Artifacts and Classes

Figure 14.21. Searching for Artifacts and Classes


Notice the available options in Figure 14.21, “Searching for Artifacts and Classes” under the Eclipse Navigate menu named Open Maven POM and Open Type from Maven. The Open Maven POM option allows you to search the Maven repository for a given POM as shown in Figure 14.22, “Searching for a POM”:

Searching for a POM

Figure 14.22. Searching for a POM


Upon selecting an artifact and clicking OK, the POM for that artifact is opened in Eclipse for browsing or editing. This is handy when you need to take a quick look at the POM for a given artifact.

The second m2eclipse option in the Navigate menu is named Open Type from Maven. This feature allows you to search for a Java class by name in a remote repository. Upon opening this dialog, simply type ‘factorybean’ and you’ll see many classes with the name FactoryBean in them as shown in Figure 14.23, “Searching the Repository for a Class”:

Searching the Repository for a Class

Figure 14.23. Searching the Repository for a Class


This is a big time saving feature because it means that manually searching through artifacts in a Maven repository for a particular class is a thing of the past. If you need to use a specific class, just fire up Eclipse, go to the Navigate menu and search for the class. m2eclipse will show you the list of artifacts in which it appears.

14.10.2. Indexing Maven Repositories

Favicon

The Maven Indexes View allows you to manually navigate to POMs in a remote repository and open them in Eclipse. To see this View, go to ViewShow ViewOther, type the word "maven" into the search box and you should see a view named Maven Indexes as shown in Figure 14.24, “Show Maven Indexes View”:

Show Maven Indexes View

Figure 14.24. Show Maven Indexes View


Select this View and click OK. This will show the Maven Indexes View as shown in Figure 14.25, “Maven Indexes View”:

Maven Indexes View

Figure 14.25. Maven Indexes View


Additionally, Figure 14.26, “Locating a POM from the Indexes View” shows the Maven Indexes View after manually navigating to locate a POM:

Locating a POM from the Indexes View

Figure 14.26. Locating a POM from the Indexes View


After finding the apache-camel artifact, double-clicking on it will open it up in Eclipse for browsing or editing.

These features make working with remote repositories from inside of Eclipse so much easier and faster. After all the hours you may have spent doing these types of tasks by manually over the last few years - visiting repositories through a web browser, downloading artifacts and grepping through them for classes and POMs - you'll find that m2eclipse is a welcome change for the better.

14.11. Using the Form-based POM Editor

Favicon

The latest release of the m2eclipse plugin has a form-based POM editor which allows you to edit every part of a project's pom.xml with an easy-to-use GUI interface. To open the POM Editor, click on a project's pom.xml file. If you've customized the editors for a pom.xml file, and the POM Editor is not the default editor, you may need to right-click on the file and choose "Open With... / Maven POM Editor". The POM Editor will then display the Overview tab as shown in Figure 14.27, “Overview Tab of POM Editor for idiom-core”.

One common complaint about Maven is that it forces a developer to confront large and often overwhelming XML documents in a highly complex multi-module project build. While the authors of this book believe this is a small price to pay for the flexibility of a tool like Maven, the graphical POM editor is a tool that makes it possible for people to use Maven without ever having to know about the XML structure behind a Maven POM.

Overview Tab of POM Editor for idiom-core

Figure 14.27. Overview Tab of POM Editor for idiom-core


The project shown in Figure 14.27, “Overview Tab of POM Editor for idiom-core” is a project with an artifactId of of idiom-core. You'll notice that most of the fields in this idiom-core project are blank. There is no groupId or version and there is no SCM information supplied in the POM editor. This is due to the fact that idiom-core inherits most of this information from a parent project named idiom. If we open the pom.xml for the parent project in the POM Editor we would see the Overview tab shown in Figure 14.28, “Overview Tab of POM Editor for idiom Parent Project”.

That “open folder” icon on the various list entries throughout the POM editor indicate that the corresponding entry is present in the Eclipse workspace and “jar” icon indicates artifacts which are referenced from the Maven repository. You can double-click on those entries in order to open its POM in the POM editor. This works for modules, dependencies, plugins and other elements that have corresponding Maven artifacts. Underlined labels in several POM editor sections represent hyperlinks which can be used to open the POM editor for corresponding Maven artifact.

Overview Tab of POM Editor for idiom Parent Project

Figure 14.28. Overview Tab of POM Editor for idiom Parent Project


In this parent POM, we see that the groupId and version are defined and that the parent POM supplies much of the information which was missing in the idiom-core project. The POM editor is going to show you the contents of the POM that you are editing, and it will not show you any of the inherited values. If you wanted to look at the idiom-core project's effective POM in the POM editor, you can use “Show Effective POM” action from the tool-bar in the upper right-hand corner of the POM editor, which shows a left bracket and an equals sign on a page with a blue M. It will load the effective POM for idiom-code in the POM Editor as shown in Figure 14.29, “Effective POM for idiom-core”.

Effective POM for idiom-core

Figure 14.29. Effective POM for idiom-core


This effective view of the POM merges the idiom-core POM with the ancestor POMs (the parent, the grandparent, etc.), similarly to “mvn help:effective-pom” command and displays the POM editor with the effective values. Because the POM editor is display a composite view of many different merged POMs, this effective POM Editor is read-only, and you will not be able to update any of the fields in this effective POM view.

If you were looking at the POM editor for the idiom-core project as shown in Figure 14.27, “Overview Tab of POM Editor for idiom-core”, you can also navigate to the parent POM using, ”Open Parent POM” action from the POM editor tool-bar in the upper right-hand of the POM editor.

The POM editor shows a number of showing various information from the POM. The final tab exposes the pom.xml as an XML document. There is a dependencies tab shown in Figure 14.30, “Dependencies Tab of the POM Editor” which exposes an easy-to-use interface for adding and editing dependencies to your project, as well as editing the dependencyManagement section of the POM. This dependency management screen is also integrated with the artifact searching facilities in the m2eclipse plugin. You can use actions from the editor sections, as well as Ctrl-Space typing assistance for the fields in “Dependency Details” section.

If you need to know more about one of the artifacts, you can use “Open Web Page” action from “Dependency Details” section tool-bar to check the project web page.

Dependencies Tab of the POM Editor

Figure 14.30. Dependencies Tab of the POM Editor


The build tab shown in Figure 14.31, “Build Tab of the POM Editor” provides access to the contents of the build element. From this tab you can customize source directories, add extensions, change the default goal name, and add resources directories.

Build Tab of the POM Editor

Figure 14.31. Build Tab of the POM Editor


We only showed a small subset of the POM editor. If you are interested in seeing the rest of the tabs, please download and install the m2eclipse plugin.

14.12. Analyzing Project Dependencies in m2eclipse

Favicon

The latest release of m2eclipse contains a POM editor which provides some dependency analysis tools. These tools promise to change the way people maintain and monitor a project's transitive dependencies. One of the main attractions to Maven is the fact that it manages a project's dependencies. If you are writing an application which depends on the Spring Framework's Hibernate3 integration, all you need to do is depend on the spring-hibernate3 artifact from the Central Maven Repository. Maven then reads this artifact's POM and adds all of the necessary transitive dependencies. While this is a great feature that attracts people to using Maven in the first place, it can often become confusing with a project starts to depend on tens of dependencies, each with tens of transitive dependencies.

Problems start to happen when you depend on a project with a poorly crafted POM which fails to flag dependencies as optional, or when you start encountering conflicts between transitive dependencies. If one of your requirements is to exclude a dependency like commons-logging or the servlet-api, or if you need to find out why a certain dependency is showing up under a specific scope you will frequently need to invoke the dependency:tree and dependency:resolve goals from the command-line to track down the offending transitive dependencies.

This is where the POM editor in m2eclipse comes in handy. If you open a project with many dependencies, you can open the Dependency Tree tab and see a two-column display of dependencies as shown in Figure 14.32, “Dependency Tree Tab of the POM Editor”. The left-side of the panel displays a tree of dependencies. The first level of the tree consists of direct dependencies from your project, and each subsequent level lists the dependencies of each dependency. The left-hand side is a great way to figure out how a specific dependency made its way into your project's resolved dependencies. The right-hand side of this panel displays the resolved dependencies. This is the list of effective dependencies after all conflicts and scopes have been applied, and it is the effective list of dependencies that your project will use for compilation, testing, and packaging.

Dependency Tree Tab of the POM Editor

Figure 14.32. Dependency Tree Tab of the POM Editor


The feature which makes the Dependency Tree tab so valuable is that it can be used as an investigative tool to figure out how a specific dependency made it into the list of resolved dependencies. Searching and filtering functionality available in the editor makes it really easy to search and browse trough the project dependencies. You can use “Search” entry field from the editor tool-bar and “Sort” and “Filter” actions from “Dependency Hierarchy” and “Resolved Dependencies” sections to navigate trough dependencies. Figure 14.33, “Locating Dependencies in the Dependency Tree” shows what happens when you click on commons-logging in the "Resolved Dependencies" list. When filtering is enabled in “Dependencies Hierarchy” section, clicking on a resolved dependency filters the hierarchy on the left-hand side of the panel to show all of the node which contributed to the resolved dependency. If you are trying to get rid of a resolved dependency, you can use this tool to find out what dependencies (and what transitive dependencies) are contributing the artifact to your resolved dependencies. In other words, if you are trying to get rid of something like commons-logging from your dependency set, the Dependency Tree tab is the tool you will likely want to use.

Locating Dependencies in the Dependency Tree

Figure 14.33. Locating Dependencies in the Dependency Tree


m2eclipse also provides you with the ability to view your project's dependencies as a graph. Figure 14.34, “Viewing the Dependencies of a Project as a Graph” shows the dependencies of idiom-core. The top-most box is the idiom-core project and the other dependencies are shown below it. Direct dependencies are linked from the top box and the transitive dependencies are linked from those. You can select a specific node in the graph to highlight the linked dependencies, or you can use the Search field at the top of the page to find matching nodes.

Note that “open folder” icon on each graph node indicates that the corresponding artifact is present in the Eclipse workspace and “jar” icon indicates that the node's artifact is referenced from the Maven repository.

Viewing the Dependencies of a Project as a Graph

Figure 14.34. Viewing the Dependencies of a Project as a Graph


The graph presentation can be changed by right clicking in the editor. You can choose to show artifact ids, group ids, versions, scopes, or if you want to wrap node text or show icons. Figure 14.35, “Radial Layout of Dependency Graph” shows the same graph from Figure 14.34, “Viewing the Dependencies of a Project as a Graph” with a radial layout.

Radial Layout of Dependency Graph

Figure&nbs