导入BOM pom时如何覆盖Spring Boot的版本号?

时间:2017-09-28 17:57:52

标签: maven spring-boot maven-bom

如何覆盖Spring Boot导入的版本号,而无需在依赖关系管理部分手动设置每个工件?

buff

然而,当我跑

<properties>
    <spring.boot.version>1.5.7.RELEASE</spring.boot.version>
    <jackson.version>2.9.1</jackson.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson</groupId>
            <artifactId>jackson-bom</artifactId>
            <version>${jackson.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

输出

mvn dependency:tree "-Dincludes=com.fasterxml.jackson.*" -Dverbose

其中[INFO] ------------------------------------------------------------------------ [INFO] Building dpt-domain-core 1.0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ dpt-domain-core --- [INFO] net.initech.dpt:dpt-domain-core:jar:1.0.0 [INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile [INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.8.10:compile [INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.10:compile [INFO] | +- (com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile - omitted for duplicate) [INFO] | \- (com.fasterxml.jackson.core:jackson-core:jar:2.8.10:compile - omitted for duplicate) [INFO] \- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.8.10:compile [INFO] +- (com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile - omitted for duplicate) [INFO] +- (com.fasterxml.jackson.core:jackson-core:jar:2.8.10:compile - omitted for duplicate) [INFO] \- (com.fasterxml.jackson.core:jackson-databind:jar:2.8.10:compile - omitted for duplicate) 2.8.10定义的jackson.version的值。

但是,如果我明确添加

org.springframework.boot:spring-boot-dependencies:1.5.7.RELEASE:pom

到我的依赖关系管理 部分,然后它正确解析为:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>

这令人困惑,因为它似乎应该等同于[INFO] ------------------------------------------------------------------------ [INFO] Building dpt-domain-core 1.0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ dpt-domain-core --- [INFO] org.autodatacorp.dpt:dpt-domain-core:jar:1.0.0 [INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.1:compile [INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.9.1:compile [INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.1:compile [INFO] \- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.1:compile [INFO] ------------------------------------------------------------------------ import应该是等效将该代码的内容粘贴到手动

我甚至尝试过

com.fasterxml.jackson:jackson-bom:2.9.1:pom

但没有效果。

PS - 重要的是,我使用的Maven是:

<dependency>                                                                                  
    <groupId>org.springframework.boot</groupId>                                               
    <artifactId>spring-boot-dependencies</artifactId>                                         
    <version>${spring.boot.version}</version>                                                 
    <exclusions>                                                                              
        <exclusion>                                                                           
            <groupId>com.fasterxml.jackson</groupId>                                          
            <artifactId>jackson-bom</artifactId>                                              
        </exclusion>                                                                          
    </exclusions>                                                                             
    <type>pom</type>                                                                          
    <scope>import</scope>                                                                     
</dependency>                                                                                 

2 个答案:

答案 0 :(得分:5)

  1. 您可以重新订购BOM导入,这将有效。在Spring Boot BOM之前放置Jackson BOM。快速示例https://github.com/Flaw101/gsonconverter/blob/feature/jackson_override/pom.xml
  2. 如果您使用Spring Boot Parent POM,您只需覆盖其属性jackson.version以覆盖其他框架/库的版本
  3. Spring Boot记录了这一点,

    https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-parent-pom

    https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent

答案 1 :(得分:0)

jackson-bom.version 添加到 pom.xml 文件的属性部分应该会更新 jackson 依赖项。这将覆盖 Spring Boot Parent POM 中的 jackson 版本。

<properties>
    <jackson-bom.version>2.12.1</jackson-bom.version>
</properties>

使用 jackson.version 是行不通的。请参阅https://github.com/spring-projects/spring-boot/issues/17808