Developing with Eclipse and Maven

3.3. Importing Maven Projects

3.3. Importing Maven Projects

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 3.9, “Importing a Maven Project” shows the wizard for importing projects with the options for Maven provided by m2eclipse:

Importing a Maven Project

Figure 3.9. Importing a Maven Project

The dialog in Figure 3.9, “Importing a Maven Project” is displayed by using the File, Import 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.

3.3.1. Importing a Maven Project

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 3.10, “Importing a Multi-module Maven Project”.

Importing a Multi-module Maven Project

Figure 3.10. Importing a Multi-module Maven Project

Figure 3.10, “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.

3.3.2. Materializing a Maven Project

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 3.11, “Materializing a Maven Project” shows the wizard after choosing to materialize Maven projects:

Materializing a Maven Project

Figure 3.11. Materializing a Maven Project

Notice that the dialog box for Maven artifacts in Figure 3.11, “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 3.12, “Selecting Artifact to Materialize” shows how to add a project:

Selecting Artifact to Materialize

Figure 3.12. 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 3.13, “Materializing Apache Camel”.

Materializing Apache Camel

Figure 3.13. Materializing Apache Camel

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