springboot依赖管理

问题引入:

springboot工程A依赖工程B, B中定义 jackson 的依赖版本为 2.11.2, 在A中除去[spring-boot-starter-web:2.1.3.RELEASE]对 jackson 的依赖. 查看工程A依赖的 jsckson 版本, 结果是:2.9.8

这是为什么呢? 答案要从 springboot 依赖管理探寻. 我们知道一个 sptingboot 工程的 pom 文件都有如下配置:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>

 

查看 spring-boot-starter-parent-2.1.3.RELEASE.pom, 它又继承于 spring-boot-dependencies

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

 

查看 spring-boot-dependencies-2.1.3.RELEASE.pom

    <properties>
        ...
        <jackson.version>2.9.8</jackson.version>
        ...
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>2.1.3.RELEASE</version>
            </dependency>
            ...
        </dependencies>
    </dependencyManagement>

 

看到这里, 答案似乎跃然纸上了. 是 springboot 工程A继承了 spring-boot-dependencies 的 pom 文件中的 properties, 导致覆盖了从B工程依赖的 jackson 版本.

再进一步思考,如果我想升级 springboot 的 jackson 版本该怎么做呢? 答案很简单, 只需要在 springboot 工程 pom 文件的 properties 标签下增加如下代码:

<jackson.version>2.11.2</jackson.version>

 

彩蛋:

relativePath 标签

The relative path of the parent pom.xml file within the check out. If not specified, it defaults to ../pom.xml. Maven looks for the parent POM first in this location on the filesystem, then the local repository, and lastly in the remote repo. relativePath allows you to select a different location, for example when your structure is flat, or deeper without an intermediate parent POM. However, the group ID, artifact ID and version are still required, and must match the file in the location given or it will revert to the repository for the POM. This feature is only for enhancing the development in a local checkout of that project. Set the value to an empty string in case you want to disable the feature and always resolve the parent POM from the repositories.
Default value is: ../pom.xml.

http://maven.apache.org/ref/3.0/maven-model/maven.html

 

Inheritance 继承, 阐述了 children 可以继承哪些 parent POM elements

Most elements from the parent POM are inherited by its children, including: properties...
https://maven.apache.org/pom.html#Inheritance

 

posted @ 2020-09-25 17:53  景岳  阅读(844)  评论(0编辑  收藏  举报