导入时如何定制spring-boot依赖项的版本

spring-boot通过maven的依赖管理为我们写好了很多依赖项及其版本,我们可拿来使用。spring-boot文档介绍了两种使用方法,一是继承,二是导入。

通过<parent>继承:

<project>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.9.RELEASE</version>
  </parent>
</project>

或者在<dependencyManagement>中导入:

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

此外,在其 文档 里说到,继承时可简单地通过属性定制依赖项版本。比如,改为使用较新的spring-4.1.6.RELEASE版本:

<properties>
    <spring.version>4.1.6.RELEASE<spring.version>
</properties>

不过,此法只对继承有效,导入无效。以下摘自其文档说明:

This only works if your Maven project inherits (directly or indirectly) from spring-boot-dependencies. If you have added spring-boot-dependencies in your own dependencyManagement section with <scope>import</scope> you have to redefine the artifact yourself instead of overriding the property.

导入时有没有较简单的方法呢?我们可先继承后导入!

1、先建一个过渡性工程,继承后定制依赖项的版本。

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>1.1.9.RELEASE</version>
  </parent>
  <groupId>mycomp</groupId>
  <artifactId>myproject-spring-boot-bom</artifactId>
  <version>1.1.9</version>

  <packaging>pom</packaging>

  <properties>
    <spring.version>4.1.6.RELEASE</spring.version>
  </properties>
</project>

2、然后导入到自己的工程里。

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>mycomp</groupId>
  <artifactId>myproject</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>mycomp</groupId>
        <artifactId>myproject-spring-boot-bom</artifactId>
        <version>1.1.9</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

这样,虽然多建了一个过渡性工程,但定制依赖项版本同继承时一样简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值