maven dependency management mechanism, the priority about dependency management which using import scope

Viewed 706

There are two examples.


example 1.

pom of project A is a simple pom:

 <groupId>org.demo</groupId>
<artifactId>kafka-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>kafka-demo</name>

pom of project B:

    <parent>
    <artifactId>kafka-demo</artifactId>
    <groupId>org.demo</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>child-one</artifactId>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <scope>compile</scope>
    </dependency>
</dependencies>

In this example, spring-boot version of project B is 2.0.3.RELEASE.

example 2.

pom of project A.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/>
</parent>

<groupId>org.demo</groupId>
<artifactId>kafka-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>kafka-demo</name>

pom of project B is the same as the previous example.

<parent>
    <artifactId>kafka-demo</artifactId>
    <groupId>org.demo</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>child-one</artifactId>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <scope>compile</scope>
    </dependency>
</dependencies>

In the example two, spring-boot version of project B is 2.3.1.RELEASE.


My question is in example 2, why the version of spring-boot is 2.3.1, rather than 2.0.3.

This scope is only supported on a dependency of type pom in the section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

1 Answers

AFAIK, dependencyManagement that is directly declared in the POM is always stronger than imported dependencyManagement.

It is wrong to assume that an import is the same as declaring the list from the BOM at the same place.

So in your second example, one of the BOMs is used as parent, so it is directly used in the resulting effective POM, while the other is an import, which is less strong.

Related