In this article by Sanjay Shah, author of the book Maven for Eclipse, we will learn the following topics:
- The Maven project structure
- Downloading Maven
- Maven versus Ant
- Creating a Maven project
- Checking out and importing a Maven project
- The Maven project build architecture
- POM (Project Object Model)
- POM relationships
- Project dependencies
- Dependency scopes
- Plugins and goals
- Installing Maven
- Writing unit tests
- Generating site documentation and HTML reports
- m2eclipse preferences
(For more resources related to this topic, see here.)
The Maven project structure
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:
Maven versus Ant
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.
Creating a Maven project
m2eclipse makes the creation of Maven projects simple. Maven projects can be created in the following two ways:
- Using an archetype
- Without using an archetype
Using an archetype
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:
- Maven plugins
- Simple web applications
- Simple projects
Checking out a Maven project
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
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 Maven project build architecture
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 (Project Object Model)
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:
- Project information: This provides general information of the project such as the project name, URL, organization, list of developers and contributors, license, and so on.
- POM relationships: In rare cases, a project can be a single entity and does not depend on other projects. This section provides information about its dependency, inheritance from the parent project, its sub modules, and so on.
- Build settings: These settings provide information about the build configuration of Maven. Usually, behavior customization such as the location of the source, tests, report generation, build plugins, and so on is done.
- Build environment: This specifies and activates the build settings for different environments. It also uses profiles to differentiate between development, testing, and production environments.
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:
The local repository
A local repository is one that resides in the same machine where a Maven build runs.
The central repository
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.
The remote repository
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.
Plugins and goals
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:
- Setting up Maven home, that is, the M2_HOME variable
- Adding Maven home to the PATH variable
Writing unit tests
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.
Generating site documentation
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.
Generating unit tests – HTML reports
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.
Other features in m2eclipse
The available features are as follows:
- Add Dependency
- Add Plugin
- New Maven Module Project
- Download JavaDoc
- Download Sources
- Update Project
- Disable Workspace Resolution
- Disable Maven Nature
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.
A form-based POM editor
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.
Analyzing project dependencies
A POM editor has a Dependencies tab that provides a glance of dependencies and an option to manage dependencies of the project.
Working with repositories
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:
- Local Repositories
- Global Repositories
- Project Repositories
- Custom Repositories
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:
- Maven: It allows us to set various options for the maven such as Offline, Debug Output, Download artifact sources, Download artifact JavaDoc, and so on.
- Archetypes: It allows to add, remove, and edit the maven archetype catalog.
- Discovery: It is used to discover the m2e connectors available for use
- Installations: It shows the maven installations and allows to choose maven to use.
- Lifecycle Mappings: It allows to customize the project build lifecycle for maven projects used by m2eclipse.
- Templates: It shows the list of all the templates used by the maven. It also provides option to add new templates, edit, remove, import, and export the templates.
- User interface and User settings: User interface allows to set XML file options and User setting allows to use the custom settings file and reindex the local repository.
- Warnings: This allows to enable/disable the warning for duplicate groupid and version across parent-child POM.
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.
Resources for Article:
- JSON to POJO Using Gson in Android Studio [article]
- Unit Testing Apps with Android Studio [article]
- Things to Consider When Migrating to the Cloud [article]