Developing with Eclipse and Maven
Chapter 7. Using m2eclipse
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
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 7.1, “Manually Adding a Dependency to the Project's POM” shows how I added a dependency
commons-lang to the
camel-console POM and the Maven
Dependencies container was automatically updated to included it:
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 7.2, “Updating Maven Dependencies”:
The second option for adding a dependency is much easier because
you don’t have to know any information about the artifact other than its
groupId. Figure 7.3, “Searching for a Dependency” shows this
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.
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.
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.
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.