Fundamentals 22 min read

Comprehensive Guide to Maven: Build Lifecycle, Dependencies, and Plugins

This article provides a detailed overview of Maven, covering its purpose, project structure, build lifecycle phases, dependency coordinates and scopes, repository types, plugin mechanisms, aggregation and inheritance, as well as flexible configuration using properties and profiles, all illustrated with practical code examples.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Comprehensive Guide to Maven: Build Lifecycle, Dependencies, and Plugins

What is Maven

Maven is a cross‑platform project management tool from the Apache foundation, primarily used for building, dependency management, and project information handling of Java‑based projects. It is analogous to yum/apt on Linux or npm in the frontend world.

What is a Build

A build consists of compiling source code, running unit tests, generating documentation, packaging, and deploying the artifacts.

Build Steps

clean : delete previously compiled class files.

compile : compile Java source files into .class bytecode.

test : run automated tests (e.g., JUnit).

report : produce test execution reports.

package : create .war for web projects or .jar for Java libraries.

install : copy the packaged artifact into the local Maven repository.

deploy : copy the artifact to a remote repository for sharing.

Project Skeleton

Typical Maven directory layout (Project Object Model - POM):

root-directory: project-name
|---src
|   |---main
|   |   |---java      // source code
|   |   |---resources // configuration files
|   |---test
|       |---java      // test code
|       |---resources // test resources
|---pom.xml           // Maven configuration file

Simple Demo

## 1. Generate a simple Maven archetype
mvn archetype:generate -DarchetypeCatalog=internal

## 2. Compile the generated project
mvn compile

## 3. Other common commands
mvn test-compile
mvn package
mvn clean
mvn install
# mvn deploy (not demonstrated)

Coordinates and Dependencies

Each artifact is uniquely identified by a set of coordinates similar to a point (x, y) in geometry:

groupId : the organization or project group.

artifactId : the specific module name.

version : the artifact version.

packaging : the packaging type (jar, war, pom, etc.).

classifier : optional qualifier for different builds of the same artifact.

Dependency Declaration

...
...
...
...
true
...
...
...

Dependency Scopes

compile : default scope, available in all classpaths.

test : only for test compilation and execution.

provided : needed for compilation but supplied by the runtime container.

runtime : required at runtime but not for compilation.

system : similar to provided but requires an explicit systemPath .

import : used inside dependencyManagement to import a BOM.

Optional Dependencies

Marking a dependency as optional prevents it from being transitively passed to downstream projects.

com.example
optional-lib
1.0
true

Repositories

Maven searches for artifacts in the following order: local repository, repositories defined in settings.xml profiles, repositories defined in the project's pom.xml , mirrors, and finally the central repository.

Lifecycle

Maven defines three independent lifecycles: clean , default , and site . Each lifecycle consists of ordered phases.

Clean Lifecycle

Phase

Description

pre-clean

Tasks before cleaning.

clean

Remove files generated by the previous build.

post-clean

Tasks after cleaning.

Default Lifecycle (selected phases)

Phase

Description

validate

Check that the project is correct and all necessary information is available.

initialize

Set up build state, e.g., define properties.

compile

Compile the main source code.

process-classes

Enhance or optimize compiled bytecode.

test-compile

Compile test source code.

test

Run unit tests.

package

Package compiled code into a distributable format (jar, war, ear).

install

Install the artifact into the local repository.

deploy

Deploy the artifact to a remote repository.

Site Lifecycle

Phase

Description

pre-site

Pre‑site generation tasks.

site

Generate project documentation site.

post-site

Post‑site generation tasks.

site-deploy

Deploy the generated site to a server.

Plugins

All real work in Maven is performed by plugins bound to lifecycle phases. A plugin can have multiple goals (targets).

Plugin Declaration Example

org.apache.maven.plugins
maven-source-plugin
2.1.1
attach-sources
verify
jar-no-fork

Custom Binding

Bind any plugin goal to any lifecycle phase, e.g., attach source JAR during the verify phase as shown above.

Plugin Configuration

Command‑line configuration using -Dproperty=value .

Global configuration inside pom.xml under <configuration> .

Aggregation and Inheritance

Aggregation ( <modules> ) builds multiple modules together. Inheritance ( <parent> ) shares common configuration such as groupId , version , dependencies, plugins, etc., while artifactId and a few other elements are not inherited.

Flexible Builds

Use properties, resource filtering, and profiles to adapt builds for different environments.

Properties

Built‑in properties: ${basedir} , ${project.version} , etc.

POM properties: ${project.build.sourceDirectory} , ${project.artifactId} , …

Custom properties defined under <properties> .

Settings, system, environment, and parent properties can also be referenced.

Profiles

Define multiple <profile> sections with activation conditions and specific dependencies, properties, or resource filters, allowing different configurations for development, testing, or production environments.

Source: https://juejin.im/post/6844903839435341832

Javaproject managementmavenpluginsdependenciesBuild Lifecycle
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.