maven依赖的版本管理

使用变量进行管理

定义一个版本号的变量

<properties>
	<spring-framework-version>4.3.7.REALEASE</spring-framework-version>
</properties>

所有spring的jar版本都使用变量来定义版本:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${spring-framework-version}</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${spring-framework-version}</version>
  <scope>test</scope>
</dependency>

使用maven的dependencyManagement管理

单个jar的管理

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
  </dependencies>
</dependencyManagement>

在引用依赖时,不需要填写版本。

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
</dependency>

在一个项目中,这样做的必要性不大,这种机制一般用于maven项目继承,子项目可以直接使用简化的依赖配置,从而确保和父项目版本一致。

这里有一个问题,如果我们配置了:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-jpa</artifactId>
  <version>1.10.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.3.7.RELEASE</version>
  <scope>test</scope>
</dependency>

会发现依赖树中,spring-test是4.3.7,而spring-data-jpa中依赖的其他spring子项目确实4.2.8,这经常会导致一些莫名其妙的问题,比如spring-test异常等等。

这个问题在使用下面的pom来管理时就可以避免了,针对spring-data-jpa项目尤其要注意。

pom管理jar集合的版本

以Spring为例,它包含大量的子项目,为了保持不同子项目的版本一致,官方提供了一个pom专门来管理版本。

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-framework-bom</artifactId>
      <version>4.3.7.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

那么大部分一级项目,都可以直接如下引用依赖了。

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <scope>test</scope>
</dependency>

之所以说大部分,如spring-data下面的子项目是Spring子项目中的一个子集。它提供了自己的pom包。

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-releasetrain</artifactId>
      <version>Hopper-SR4</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>

它的版本不同于普通的版本号,如1.11.7.RELEASE这种,而是独立的版本体系,具体版本参见:https://github.com/spring-projects/spring-data-commons/wiki/Release-planning

这个字符串的版本号,实际上又对应了真实的版本号,如:

Hopper-SR4 <-> 1.10.4.RELEASE

具体的版本对应查询前面的文档。

转载于:https://my.oschina.net/polly/blog/853764

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值