If I add a duplicate dependency in dependencyManagement, which is already managed in parent pom, dependency management only considers this last declaration, anything declared in parent is ignored.
Are there any use-cases not inherit parent dependencyManagement?
In my hypothetical example I have org.jooq:jooq:3.16.9 which depends on io.r2dbc:r2dbc-spi:0.9.0.RELEASE.
Parent POM declares a newer version io.r2dbc:r2dbc-spi:0.9.1.RELEASE. If Child POMs would use jooq they would get 0.9.1.RELEASE of io.r2dbc:r2dbc-spi. This is an expected behavior. So far so good.
If now a Child POM decides to change only the scope and does not provide a version, Maven resolves the version of io.r2dbc:r2dbc-spi to 0.9.0.RELEASE, which is declared by org.jooq:jooq:3.16.9.
(Same behavior with scope and exclusions, even when I import parent instead of inheriting.)
I expected, that Maven would resolve io.r2dbc:r2dbc-spi to 0.9.1.RELEASE since it is declared in parent dependencyManagement.
Duplicate dependencies in dependencyManagement are re-declared and not inherited:
Finally, since d is specified in B's dependency management section, should d be a dependency (or transitive dependency) of a or c, version 1.0 will be chosen - again because dependency management takes precedence over dependency mediation and also because the current POM's declaration takes precedence over its parent's declaration.
POMs
(Actual artifacts are only used for illustration purpose, there might have been other better artifacts ...)
Parent POM:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>MavenParentTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi</artifactId>
<version>0.9.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.16.9</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version> <!-- just to fix plugins version -->
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Child POM:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>MavenChildTest</artifactId>
<parent>
<groupId>org.example</groupId>
<artifactId>MavenParentTest</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
</dependency>
</dependencies>
</project>
Running: mvn dependency:tree -f child/pom.xml leads to following output:
[INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ MavenChildTest ---
[INFO] org.example:MavenChildTest:jar:1.0-SNAPSHOT
[INFO] \- org.jooq:jooq:jar:3.16.9:compile
[INFO] +- io.r2dbc:r2dbc-spi:jar:0.9.0.RELEASE:runtime
[INFO] | \- org.reactivestreams:reactive-streams:jar:1.0.3:runtime
[INFO] \- jakarta.xml.bind:jakarta.xml.bind-api:jar:3.0.0:compile
[INFO] \- com.sun.activation:jakarta.activation:jar:2.0.0:compile