English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
One of Maven's core features is dependency management. When dealing with multi-module projects (including hundreds or even thousands of modules or sub-projects), the dependency relationships between modules become very complex, and management becomes very difficult. For such situations, Maven provides a highly controlled method.
A fairly common situation, for example, if library 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 the required libraries. Maven finds the dependency relationships between projects by reading the project file (pom.xml).
All we need to do is define the direct dependency relationship well in the pom of each project. The other things Maven will take care of for us.
Through transitive dependencies, the graphical representation of all included libraries will grow rapidly. When there are duplicate libraries, the possible situations will continue to rise. Maven provides some functions to control the extent of transitive dependencies.
Function | Function Description |
---|---|
Dependency Adjustment | Decide which dependency version will be used when multiple manually created versions appear simultaneously. If two dependency versions have the same depth in the dependency tree, the first declared dependency will be used. |
Import stage | 直接的指定手动创建的某个版本被使用。例如当一个工程 C 在自己的依赖管理模块包含工程 B,即 B 依赖于 A, 那么 A 即可指定在 B 被引用时所使用的版本。 |
Dependency optional | 包含在构建过程每个阶段的依赖。 |
Directly specify the manually created version used. For example, when a project C includes project B in its dependency management module, that is, B depends on A, then A can specify the version used when B is referenced. | Dependencies included in each stage of the build process. |
Dependency exclusion | Any transitive dependency can be excluded by using the "exclusion" element. For example, A depends on B, B depends on C. Therefore, A can mark C as "excluded". |
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 A no longer needs to use C.
Dependency scope | The discovery of transitive dependency discovery can be limited by using the following dependency scope: |
---|---|
Scope | Description |
Compilation stage | This scope indicates that the relevant dependencies are only valid in the project's classpath. Default value. |
Supply stage | This scope indicates that the relevant dependencies are provided by the runtime JDK or network server. |
Runtime stage | This scope indicates that the relevant dependencies are not necessary in the compilation phase, but are necessary in the execution phase. |
Test stage | This scope indicates that the relevant dependencies are only valid in the test compilation phase and execution phase. |
System stage | This scope indicates that you need to provide a system path. |
This scope is used only when the dependency is defined in a pom. At the same time, the dependencies defined in the section of the current project's POM file can replace a specific POM.
Dependency management
App-UI-Next is the detailed explanation of the above dependency diagram:-Core-lib and App-Data-WAR depends on App
Root is lib.-Core-lib and App-Data-as the parent project of lib.
Root defines Lib in its dependency section.1、lib2 and Lib3 as a dependency.
App-UI-The pom.xml file code of 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>
App-Core-The pom.xml file code of 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>
App-Data-The pom.xml file code of 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 App-UI-When building a WAR project, Maven will find all the dependencies by traversing the dependency graph and build the application.
Through the above example, we can learn the following key concepts:
Public dependencies can be unified together using the concept of pom parent. App-Data-lib and App-Core-project> dependencies> dependency> version> .0< packaging> pom< artifactId>Root< groupId>com.companyname.groupname< modelVersion> .0.0< .0.0.xsd"> maven< xsd< maven.apache.org< http:< .0.0< POM< xsi:schemaLocation="http:< instance< XMLSchema< 00< .org"
There is no need to list the dependencies of the lib project in the App-UI-Declare Lib in W1, lib2, Lib3 It is its dependency. Maven achieves this detail by using the transitive dependency mechanism.