Dependency management is a mechanism to centralize dependency information. When there are a set of projects (or modules) that inherit a common parent, all information about the dependency can be put in the parent pom and the projects can have simpler references to them. This makes it easy to maintain the dependencies across multiple projects and reduces the issues that typically arise due to multiple versions of the same dependencies.
simple-multi-module
).junit
in the dependencyManagement
section:<dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement>
dependencies
section of the child project as follows:<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies>
mvn clean test
mvn dependency:tree
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ simple-multi-module --- [INFO] com.packt.cookbook:simple-multi-module:pom:1.0-SNAPSHOT [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Child Project 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ child --- [INFO] com.packt.cookbook:child:jar:1.0-SNAPSHOT [INFO] - junit:junit:jar:3.8.1:test
Dependencies that are specified within the dependencyManagement
section of the parent pom are available for use to all the child projects. The child project needs to choose the dependencies by explicitly specifying the required dependencies in the dependencies
section. While doing this, the child projects can omit the version
and scope
information so that they are inherited from the parent.
You may ask, "Why have the dependencyManagement
section when child projects inherit dependencies defined in the parent pom anyway?" The reason is, the parent centralizes dependencies across several projects. A child project typically needs only some of the dependencies that the parent defines and not all of them. The dependencyManagement
section allows child projects to selectively choose these.
The dependencyManagement
section also helps address any surprises of Maven's dependency mediation. Dependency mediation is what determines what version of dependency will be used when multiple versions of an artifact are encountered. However, dependencyManagement
takes precedence over dependency mediation and ensures that dependency mediation does not pick a version over the one specified in dependencyManagement
.
It should be noted that dependencies on different versions are error prone and dependencyManagement
cannot always save them from library version incompatibilities.
18.216.117.191