問題引入:
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