Maven Dependency Mechanism
The Maven dependency mechanism is one of the core features of the Apache Maven build tool; it can automatically download and manage the external libraries (JAR files) required by a project and their dependencies.
The Maven dependency mechanism greatly simplifies the Java project build process, freeing developers from manually downloading and managing various third-party libraries.
Maven stores millions of open-source libraries through the central repository (Maven Central Repository); when you declare a dependency in your project, Maven automatically downloads that library and all its dependencies from the repository.
Dependency Declaration
In pom.xml, via the
junit
junit
4.12
Basic Concepts of Maven Dependencies
1. Coordinate System (Coordinates)
Maven uses three basic coordinates to uniquely identify a dependency:
- groupId: defines the organization or company the project belongs to (such as
org.apache) - artifactId: defines the name of the project (such as
commons-lang3) - version: defines the version of the project (such as
3.12.0)
These three elements combine to form the unique identifier of a Maven dependency.
2. Dependency Scope (Scope)
Maven defines different dependency scopes, determining in which phases a dependency is available:
- compile(default): available at compile, test, and runtime
- provided: available at compile and test time, but provided by the JDK or container at runtime
- runtime: needed only at test and runtime
- test: needed only in the test compilation and execution phases
- system: similar to provided, but requires the JAR path to be specified explicitly
3. Transitive Dependencies (Transitive Dependencies)
When project A depends on project B, and project B in turn depends on project C, Maven automatically introduces project C as a dependency of project A as well. This feature of automatically handling dependency relationships is called transitive dependencies.
Transitivity rules depend on the Scope:
| Current dependency Scope \ transitive dependency Scope | compile | provided | runtime | test |
|---|---|---|---|---|
| compile | compile | - | runtime | - |
| provided | provided | provided | provided | - |
| runtime | runtime | - | runtime | - |
| test | - | - | - | - |
How to Use Maven Dependencies in a Project
1. Add a dependency in pom.xml
In a Maven project's pom.xml file, the section is used to declare project dependencies:
Example
org.apache.commons
commons-lang3
3.12.0
2. Dependency Exclusions (Exclusions)
Sometimes you may need to exclude a transitive dependency; you can use tags:
Example
com.example
example-library
1.0
org.unwanted
unwanted-dependency
3. Dependency Management (Dependency Management)
In a multi-module project, you can use the in the parent POM to manage dependency versions uniformly:
Example
org.springframework
spring-core
5.3.20
When a child module references it, it only needs to declare the groupId and artifactId, without specifying a version.
Advanced Dependency Management Features
Dependency Version Management
Use dependencyManagement to manage versions uniformly:
org.springframework
spring-core
5.3.18
org.springframework
spring-core
BOM Import
Manage the versions of a group of related dependencies:
org.springframework.boot
spring-boot-dependencies
2.6.4
pom
import
Optional Dependencies (Optional)
Mark a dependency as optional, non-transitive:
com.example
optional-lib
1.0
true
Maven Dependency Resolution Mechanism
1. Dependency Mediation (Dependency Mediation)
When version conflicts occur, Maven resolves them using the following rules:
- Nearest definition wins (the version with the shortest path in the dependency tree is selected)
- If path lengths are equal, the first declared dependency wins
2. Effect of Dependency Scope
Dependencies of different scopes affect transitivity:
- compile-scope dependencies are transitive
- provided and test-scope dependencies are not transitive
- runtime-scope dependencies are transitive with runtime scope
Final packaging results of dependencies with different Scopes:
| Scope | Whether packaged | Typical application |
|---|---|---|
compile |
Is | Core dependency |
provided |
No | Provided by container |
runtime |
Is | Needed at runtime |
test |
No | Unit testing |
3. Optional Dependencies (Optional Dependencies)
Dependencies marked as optional are not transitive:
Example
com.example
optional-lib
1.0
true
Dependency-Related Commands
View the dependency tree:
mvn dependency:tree
Analyze dependency issues:
mvn dependency:analyze
Download dependencies to a directory:
mvn dependency:copy-dependencies
Maven Repository (Repository)
1. Repository Types
- Local repository: located under the user's home directory
.m2/repositorydirectory - Central repository: Maven's default public repository
- Remote repository: a private repository set up by a company or organization
2. Repository Configuration
can be done during pom.xml or settings.xml configure a repository in:
Example
my-repo
http://repo.example.com/maven2
Best Practices
- Specify dependency versions explicitly: avoid dynamic versions such as LATEST or RELEASE
- Update dependencies regularly: When using
mvn versions:display-dependency-updatesCheck for available updates - Use a BOM: for large frameworks (such as Spring), use a Bill of Materials to manage versions uniformly
- Remove unused dependencies: regularly run
mvn dependency:analyzecheck for unused dependencies