Page cover image

Maven

Introduction

Maven is a project management tool which encompasses a project object model, a set of standards, a project lifecycle, a dependency management system, and logic for executing plugin goals at defined phases in a lifecycle.

With Maven, a development team can quickly establish standard workflows and build deployable artifacts from the source code. The goal of using Maven is to decrease the number of decisions that a development team is required to make without necessarily losing flexibility.

Key features

  1. easy project setup that follows best practices: Maven provides project templates called archetypes to minimize configuration needs

  2. dependency management: Maven automatically updates, downloads, and validates compatibility, while also reporting dependency closures (also referred to as transitive dependencies).

  3. separation of project dependencies and plugins: There is a clear separation between project dependencies and plugins. Project dependencies are retrieved from dependency repositories, while plugin dependencies are retrieved from plugin repositories. This setup reduces conflicts, particularly when plugins need to download additional dependencies.

  4. central repository system: project dependencies can be retrieved from the local file system or public repositories like Maven Central

Maven uses Convention over Configuration approach, which means the development team is not required to create build process themselves.

Archetypes

An Archetype is a simple artifact that helps in the generation of project structure.

Archetype helps create Maven project templates for users, allowing them to generate customized versions. Using archetypes is a quick way to align developers with the best practices of a project or organization. Once deployed in the organization's repository, these archetypes are available for all developers to use.

The POM

The XML representation of a project, known as the POM (Project Object Model), encapsulates all metadata relevant to the project. The POM defines the nature of the project and how should Maven adjust default settings for generating output from the source. Essentially, it serves as a descriptive blueprint for Maven, a figurative “map” that Maven needs to understand what it is looking at when it builds the project.

Dependency Mechanism

Most projects depend on others to build and run correctly.

Maven downloads and links the dependencies on compilation, as well as on other goals that require them.

Maven also brings in the dependencies of those dependencies (transitive dependencies), allowing the project’s list to focus solely on the dependencies that this project requires.

Use the dependency:analyze and dependency:tree goals to help identify issues in your project's dependency management.

Transitive Dependencies

A transitive dependency is a dependency of a dependency.

Maven eliminates the need to manually identify and declare dependencies that project’s dependencies require by automatically including transitive dependencies . Maven achieves this by constructing a dependency graph and managing any conflicts or duplications that may arise.

Do not rely on transitive dependencies if those are used directly in your code - explicitly list a dependency for classes referenced in your code.

Dependency management

The dependency management section is a mechanism for centralizing dependency information.

When dealing with a group of projects inheriting from a shared parent, it becomes feasible to consolidate all dependency information in the common POM, simplifying references to artifacts in the child POMs.

dependencyManagement element is usually in a top-level parent POM for an organization or project. Using the dependencyManagement element in a pom.xml allows referencing a dependency in a child project without having to explicitly list the version (the version number propagates to the child project's dependency automatically).

Keep a set of dependencies which are logically grouped together in one place (consider creating module with packaging type pom).

BOM

BOM, short for Bill Of Materials, is a distinct type of POM utilized for managing the versions of a project's dependencies and serving as a centralized location for defining and updating those versions. It is commonly employed in scenarios where multiple projects inherit a common parent, allowing for the consolidation of dependency information within a shared BOM file. This approach ensures that the dependencies have undergone thorough testing to ensure compatibility.

Dependency Version

Maven versions use the following format:

<major version>.<minor version>.<incremental version>-<qualifier>

Snapshot Version

When a version includes the term SNAPSHOT, the project is under active development. Maven automatically replaces the mentioned qualifier with a UTC date and time value upon installation or release of the component. During the build process, Maven regularly attempts to download the most recent snapshot from a repository.

Use SNAPSHOT versions during development, but when releasing the package, make sure that the project is not using SNAPSHOT versions of its dependencies.

Last updated