Spring Boot的每个发布版本都会规划它所支持的依赖项。实际上,你不用指定这些依赖项的版本号,因为Spring Boot都为你管理好了。当更新Spring Boot时,会相应的更新依赖。
继承spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
只需要在这里指定Spring Boot的版本号。如果导入其他的starters,你可以完全省略版本号。
使用这个配置,你还可以通过property覆盖内部的依赖。例如,在pom.xml中升级Spring Data release train。
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
使用spring-boot-dependencies
可能有人不喜欢继承spring-boot-starter-parent POM。你可能有自己的企业标准parent,或者你可能只是比较喜欢明确声明所有的Maven配置。
如果你不想使用spring-boot-starter-parent,你依然可以通过使用scope=import利用依赖管理的便利:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
</dependency>
</dependencies>
这种方式不能使用property的形式覆盖原始的依赖项。要达到同样的效果,需要在dependencyManagement里面的spring-boot-dependencies之前添加依赖的东西。例如,要升级Spring Data release train,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>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>