<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
如果你不想使用某个依赖默认的版本,您还可以通过覆盖自己的项目中的属性来覆盖各个依赖项,例如,要升级到另一个Spring Data版本系列,您可以将以下内容添加到pom.xml中。
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
详细依赖可查看:spring-boot-parent-1.5.3.RELEASE.pom
但是如果项目有自己的继承(有自己的parent),则需要如下:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
该设置不允许您使用如上所述的属性(properties)覆盖各个依赖项,要实现相同的结果,您需要在spring-boot-dependencies项之前的项目的dependencyManagement中添加一个配置,例如,要升级到另一个Spring Data版本系列,您可以将以下内容添加到pom.xml中。
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
详细依赖可查看:spring-boot-dependencies-1.5.3.RELEASE.pom。spring-boot-dependencies 根本就没有对应的jar包,它只是一个 pom。
配置有了它之后,我们在 子项目中使用到的相关依赖,就不需要声明version了。
但是在子项目的 dependencies 中,不需要(也不能)再次添加对 spring-boot-dependencies 的声明了,否则 子项目 将无法编译通过。
需要注意的是,导入后再重新定义既繁琐又不安全。比如,spring-boot定义了8个tomcat依赖项,如果你导入后只重定义了部分tomcat依赖项及其版本,则将造成版本不一致的问题。
那么导入时有没有既简单又安全的方法呢?我们可先继承后导入!
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>
这样,虽然多建了一个过渡性工程,但定制依赖项版本同继承时一样简单。