Maven Dependencies
Maven dependency mechanism is one of the core features of Apache Maven build tool, which can automatically download and manage external libraries (JAR files) required by the project and their dependencies.
Maven dependency mechanism greatly simplifies the build process of Java projects, 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 will automatically download the library and all its dependencies from the repository.
### Dependency Declaration
Declare in pom.xml using tag:
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 (e.g., `org.apache`)
* **artifactId**: Defines the name of the project (e.g., `commons-lang3`)
* **version**: Defines the version of the project (e.g., `3.12.0`)
These three elements combine to form the unique identifier for Maven dependencies.
### 2. Dependency Scope
Maven defines different dependency scopes that determine in which phases the dependency is available:
* **compile** (default): Available for compilation, testing, and runtime
* **provided**: Available for compilation and testing, but provided by JDK or container at runtime
* **runtime**: Only needed during testing and runtime
* **test**: Only needed during test compilation and execution phases
* **system**: Similar to provided, but requires explicit JAR path specification
### 3. Transitive Dependencies
When Project A depends on Project B, and Project B depends on Project C, Maven will automatically include Project C as a dependency of Project A. This automatic dependency relationship handling is called transitive dependencies.
The transitive rules depend on 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 Projects
### 1. Add Dependencies 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
Sometimes you may need to exclude a transitive dependency, you can use the `` tag:
## Example
com.example
example-library
1.0
org.unwanted
unwanted-dependency
### 3. Dependency Management
In multi-module projects, you can use `` in the parent POM to centrally manage dependency versions:
## Example
org.springframework
spring-core
5.3.20
When sub-modules reference, they only need to declare groupId and artifactId, without specifying the version.
* * *
## Advanced Dependency Management Features
### Dependency Version Management
Use dependencyManagement to centrally manage versions:
org.springframework spring-core 5.3.18 org.springframework spring-core
### BOM Import
Manage versions of a group of related dependencies:
org.springframework.boot spring-boot-dependencies 2.6.4 pom import
### Optional Dependencies
Mark dependencies as optional, not transitive:
com.example optional-lib 1.0 true
* * *
## Maven Dependency Resolution Mechanism
### 1. Dependency Mediation
When version conflicts occur, Maven uses the following rules to resolve:
1. Nearest definition first
YouTip