Backend Development 22 min read

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

This article provides a detailed overview of Maven, covering its purpose, core concepts such as coordinates and repositories, the build lifecycle phases, as well as dependency declarations and scopes, as well as plugin configuration, custom bindings, and advanced features like aggregation, inheritance, and profile-based builds.

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

What is Maven

Maven is a cross‑platform project management and build automation tool maintained by the Apache Software Foundation, primarily serving Java‑based projects for compilation, dependency management, and project information handling.

What is a Build

A build comprises compilation, unit testing, documentation generation, packaging, and deployment.

clean : delete previously compiled class files.

compile : compile Java source files into .class bytecode.

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

report : generate test execution reports.

package : create .jar or .war archives.

install : copy the artifact to the local repository.

deploy : upload the artifact to a remote repository.

Project Skeleton

根目录:工程名
|---src
|   |---main
|   |   |---java   // 主代码
|   |   |---resources // 主资源文件
|   |---test
|       |---java   // 测试代码
|       |---resources // 测试资源文件
|---pom.xml // Maven 配置文件

Simple Demo

## 1. 使用 archetype 命令生成 Maven 简单骨架
mvn archetype:generate -DarchetypeCatalog=internal

## 2. 编译当前生成的项目
mvn compile

## 3. 其他常用命令
mvn test-compile
mvn package
mvn clean
mvn install
mvn depoly   // 暂不演示

Coordinates and Dependencies

Coordinates uniquely identify an artifact and consist of groupId , artifactId , version , packaging , and optionally classifier .

Dependency Elements

groupId : defines the organization or project the artifact belongs to.

artifactId : the module name within the project.

packaging : jar, war, pom, etc. (default is jar).

version : the artifact version.

classifier : distinguishes artifacts built from the same module (e.g., JDK version, sources, javadoc).

Dependency Declaration

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

Dependency Scopes

compile : default, available in all classpaths.

test : only for test compilation and execution.

provided : needed for compile/test but supplied by the runtime container.

runtime : needed at runtime but not for compilation.

system : explicitly points to a local file via systemPath (use with caution).

import : used inside dependencyManagement to import BOMs.

Optional Dependencies

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

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

Pre‑clean tasks.

clean

Delete files generated by the previous build.

post-clean

Post‑clean tasks.

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 main source code.

test

Run unit tests.

package

Package the 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

Tasks before site generation.

site

Generate project documentation site.

post-site

Tasks after site generation.

site-deploy

Deploy the generated site to a server.

Plugins

Actual work in each lifecycle phase is performed by plugin goals. Example of declaring the maven-source-plugin to attach source jars during the verify phase:

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

Plugin Goals

Each plugin can expose multiple goals, e.g., dependency:analyze , dependency:tree , compiler:compile , etc.

Custom Bindings

Custom bindings associate a plugin goal with any lifecycle phase. The same maven-source-plugin can be bound to verify as shown above.

Plugin Configuration

Command‑line configuration using -Dkey=value (e.g., -Dmaven.test.skip=true ).

Global configuration inside pom.xml under <configuration> (e.g., setting source/target Java version for maven-compiler-plugin ).

Aggregation and Inheritance

Aggregation groups multiple modules in a single build via the <modules> element.

Inheritance allows a child POM to inherit common configuration (groupId, version, dependencies, plugins, etc.) from a parent POM, while elements like artifactId and name are not inherited.

Flexible Builds

Using properties, resource filtering, and profiles, Maven can adapt to different environments.

Properties

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

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

Custom properties defined under <properties> .

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

Profiles

Profiles enable conditional configuration based on activation criteria (default, OS, JDK, property, etc.). They can be defined in the project POM, user settings.xml , or global settings.xml . Example snippet shows a dev profile that adds dependencies, filters, and custom properties.

Javadependency managementmavenpluginsBuild 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.