Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Maven Dependency Mechanism

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 tag declaration:





    

        junit

        junit

        4.12

    




Basic Concepts of Maven Dependencies

1. Coordinate System (Coordinates)

Maven uses three basic coordinates to uniquely identify a dependency:

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:

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:

  1. Nearest definition wins (the version with the shortest path in the dependency tree is selected)
  2. If path lengths are equal, the first declared dependency wins

2. Effect of Dependency Scope

Dependencies of different scopes affect transitivity:

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

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

  1. Specify dependency versions explicitly: avoid dynamic versions such as LATEST or RELEASE
  2. Update dependencies regularly: When using mvn versions:display-dependency-updates Check for available updates
  3. Use a BOM: for large frameworks (such as Spring), use a Bill of Materials to manage versions uniformly
  4. Remove unused dependencies: regularly run mvn dependency:analyze check for unused dependencies