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

Maven Dependency Management

Maven Dependency Management

A core feature of Maven is dependency management. When we deal with a multi-module project (containing hundreds or thousands of modules or sub-projects), the dependency relationships between modules become very complex and management becomes very difficult. For this situation, Maven provides a highly controlled approach.


Transitive Dependency Discovery

A fairly common situation, for example, A depends on another library B. If another project C wants to use A, then project C also needs to use library B.

Maven can avoid the need to search for all required libraries. Maven reads the project file (pom.xml) to find out the dependencies between projects.

All we need to do is define the direct dependencies in each project's pom. Maven will take care of everything else.

Through transitive dependencies, the graph of all included libraries grows rapidly. When there are duplicate libraries, possible situations will keep increasing. Maven provides some features to control the extent of transitive dependencies.

Function Description
Dependency Mediation Determines which dependency version will be used when multiple manually created versions appear at the same time. If two dependency versions are at the same depth in the dependency tree, the first declared dependency will be used.
Dependency Management You can directly specify a manually created version for use. For example, suppose project C includes project B as its dependency, and project B in turn depends on project A. In this case, you can explicitly specify the version of project A to use when project B is referenced. This means you can control which specific version of project A will be used by project B.
Dependency Scope Dependencies included in every phase of the build process.
Dependency Exclusion Any transitive dependency can be excluded through the "exclusion" element. For example, A depends on B, B depends on C, so A can mark C as "excluded".
Dependency Optional Any transitive dependency can be marked as optional by using the "optional" element. For example: A depends on B, B depends on C. Therefore, B can mark C as optional, so that A no longer uses C.

Dependency Scope

Transitive dependency discovery can be limited by using the following dependency scopes:

Range Description
Compile Stage This scope indicates that the dependency is only valid under the project's classpath. Default value.
Provided scope This scope indicates that the dependency is provided by the JDK at runtime or by a web server.
Runtime Stage This scope indicates that the dependency is not required at compile time, but is required at execution time.
Test scope This scope indicates that the dependency is only valid during test compilation and execution phases.
System scope This scope indicates that you need to provide a system path.
Import scope This scope is only used when the dependency is a dependency defined in a pom. Meanwhile, the current project's POM file's section defines dependencies that can replace a specific POM.

Dependency Management

Typically, under a common project, there is a series of projects. In this case, we can create a pom file of common dependencies, which contains all the common dependencies; we call it the parent pom of other child project poms. The following example can help you better understand this concept.

The following is a detailed description of the above dependency graph:

The pom.xml file code of App-UI-WAR is as follows:

<

project

xmlns

=

"

http://maven.apache.org/POM/4.0.0

"

xmlns:xsi

=

"

http://www.w3.org/2001/XMLSchema-instance

"

xsi:schemaLocation

=

"

http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd

"

<

modelVersion

4.0.0

modelVersion

<

groupId

com.companyname.groupname

groupId

<

artifactId

App-UI-WAR

artifactId

<

version

1.0

version

<

packaging

war

packaging

<

dependencies

<

dependency

<

groupId

com.companyname.groupname

groupId

<

artifactId

App-Core-lib

artifactId

<

version

1.0

version

dependency

dependencies

<

dependencies

<

dependency

<

groupId

com.companyname.groupname

groupId

<

artifactId

App-Data-lib

artifactId

<

version

1.0

version

dependency

dependencies

project

The pom.xml file code of App-Core-lib is as follows:

<

project

xmlns

=

"

http://maven.apache.org/POM/4.0.0

"

xmlns:xsi

=

"

http://www.w3.org/2001/XMLSchema-instance

"

xsi:schemaLocation

=

"

http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd

"

<

parent

<

artifactId

Root

artifactId

<

groupId

com.companyname.groupname

groupId

<

version

1.0

version

parent

<

modelVersion

4.0.0

modelVersion

<

groupId

com.companyname.groupname

groupId

<

artifactId

App-Core-lib

artifactId

<

version

1.0

version

<

packaging

jar

packaging

project

The pom.xml file code of App-Data-lib is as follows:

<

project

xmlns

=

"

http://maven.apache.org/POM/4.0.0

"

xmlns:xsi

=

"

http://www.w3.org/2001/XMLSchema-instance

"

xsi:schemaLocation

=

"

http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd

"

<

parent

<

artifactId

Root

artifactId

<

groupId

com.companyname.groupname

groupId

<

version

1.0

version

parent

<

modelVersion

4.0.0

modelVersion

<

groupId

com.companyname.groupname

groupId

<

artifactId

App-Data-lib

artifactId

<

version

1.0

version

<

packaging

jar

packaging

project

The pom.xml file code of Root is as follows:

<

project

xmlns

=

"

http://maven.apache.org/POM/4.0.0

"

xmlns:xsi

=

"

http://www.w3.org/2001/XMLSchema-instance

"

xsi:schemaLocation

=

"

http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd

"

<

modelVersion

4.0.0

modelVersion

<

groupId

com.companyname.groupname

groupId

<

artifactId

Root

artifactId

<

version

1.0

version

<

packaging

pom

packaging

<

dependencies

<

dependency

<

groupId

com.companyname.groupname1

groupId

<

artifactId

Lib1

artifactId

<

version

1.0

version

dependency

dependencies

<

dependencies

<

dependency

<

groupId

com.companyname.groupname2

groupId

<

artifactId

Lib2

artifactId

<

version

2.1

version

dependency

dependencies

<

dependencies

<

dependency

<

groupId

com.companyname.groupname3

groupId

<

artifactId

Lib3

artifactId

<

version

1.1

version

dependency

dependencies

project

Now when we build the App-UI-WAR project, Maven will find all dependencies by traversing the dependency graph and build the application.

From the above example, we can learn the following key concepts: