In this article by Sanjay Shah, author of the book Maven for Eclipse, we will learn the following topics:
(For more resources related to this topic, see here.)
Maven, as stated in earlier chapters, follows convention over configuration.
To download Maven, please visit http://maven.apache.org/download.cgi. Click on the latest version, apache-maven-x.x.x-bin.zip; at the time of writing this, the current version is apache-maven-3.2.1-bin.zip. Download the latest version as shown in the following screenshot:
Before the emergence of Maven, Ant was the most widely used build tool across Java projects. Ant emerged from the concept of creating files in C/C++ programming to a platform-independent build tool. Ant used XML files to define the build process and its corresponding dependencies.
m2eclipse makes the creation of Maven projects simple. Maven projects can be created in the following two ways:
An archetype is a plugin that allows a user to create Maven projects using a defined template known as archetype. There are different archetypes for different types of projects.
Archetypes are primarily available to create the following:
Checking out a Maven project means checking out from the source code versioning system. Before we process this, we need to make sure we have the Maven connector installed for the corresponding SCM we plan to use.
Importing a Maven project is like importing any other Java project. The steps to import a Maven project are as follows:
From the File menu, click on Import. Choose Import, a source window appears, expand Maven and click on Existing Maven Projects as shown in the following screenshot:
In the next wizard, we have to choose the Maven project's location. Navigate to the corresponding location using the Browse...button, and click on Finish to finish the import as shown in the following screenshot; the project will be imported in the workspace:
The following figure shows the common build architecture for Maven projects.
Essentially, every Maven project contains a POM file that defines every aspect of the project essentials. Maven uses the POM details to decide upon different actions and artifact generation.
POM stands for Project Object Model. It is primarily an XML representation of a project in a file named pom.xml. POM is the identity of a Maven project and without it, the project has no existence. It is analogous to a Make file or a build.xml file of Ant.
In a nutshell, the contents of POM fall under the following four categories:
POM relationships identify the relationship they possess with respect to other modules, projects, and other POMs. This relationship could be in the form of dependencies, multimodule projects, parent-child also known as inheritance, and aggregation.
A Maven repository can be one of the following types:
A local repository is one that resides in the same machine where a Maven build runs.
The central repository is the repository provided by the Maven community. It contains a large repository of commonly used libraries. This repository comes into play when Maven does not find libraries in the local repository. The central repository can be found at: http://search.maven.org/#browse.
Enterprises usually maintain their own repositories for the libraries that are being used for the project. These differ from the local repository; a repository is maintained on a separate server, different from the developer's machine and is accessible within the organization.
The powerful feature of Maven is its dependency management for any project. Dependencies may be external libraries or internal (in-house) libraries/project.
Dependency scopes control the availability of dependencies in a classpath and are packaged along with an application.
Maven, essentially, is a plugin framework where every action is the result of some plugin. Each plugin consists of goals (also called Mojos) that define the action to be taken. To put it in simple words, a goal is a unit of work. For example, a compiler plugin has compile as the goal that compiles the source of the project.
Maven's installation is a simple two-step process:
Writing unit tests is a part of good practice in software development. Maven's test phase executes unit tests and generates the corresponding report. In this section, we will learn about writing a simple unit test for our utility class ConversionUtil, and in the next section, we will see how to execute it and generate reports.
One of the integral features of Maven is that it eases artifacts and site documentation generation. To generate site documentation, add the following dependency in the pom file.
In the preceding section, we ran the unit tests, and the results were generated in the txt and xml format. Often, developers need to generate more readable reports. Also, as a matter of fact, the reports should be a part of site documentation for better collaboration and information available in one place.
The available features are as follows:
It allows us to add dependencies to the Maven project. Up until now, we have been editing the pom.xml file and adding dependencies to it.
m2eclipse provides the option of editing the pom file using a form-based POM editor. In earlier chapters, we played with XML tags and edited the pom file. While directly editing an XML file, the knowledge of tags is required, and there is a high chance that the user will make some errors. However, a form-based editor reduces the chance of a simple error and eases the editing of a pom file without or very minimal XML knowledge behind the scene.
A POM editor has a Dependencies tab that provides a glance of dependencies and an option to manage dependencies of the project.
To browse through the repository, navigate to Window | Show View and click on Other...as follows:|
The Maven Repositories view constitutes of the following types:
To open m2eclipse preferences, navigate to Window | Preferences. In the Preferences window and search for maven in the filter textbox as follows:
The available Maven preferences are as follows:
In this article we studied the Maven project structure, how to download and import a Maven project also using m2eclipse, and available preferences of Maven.
Further resources on this subject: