Spring Boot的每个发布版本都会规划它所支持的依赖项。实际上,你不用指定这些依赖项的版本号,因为Spring Boot都为你管理好了。当更新Spring Boot时,会相应的更新依赖。
Maven管理依赖
Maven用户可以继承spring-boot-starter-parent项目,来获取最佳依赖。这个父项目提供了以下几个功能:
- 默认Java 1.6编译
- UTF-8编码格式
- 依赖管理部分,可让你对公共依赖省略version标签。继承自spring-boot-dependencies POM。
- 良好的资源过滤
- 良好的插件配置
- 对于application.properties和application.yml包括profile-specific文件,良好的资源过滤
最后一点:因为默认配置文件接受Spring风格的占位符(${}),Maven过滤器换成了@...@占位符。(可以通过Maven属性resource.delimiter替换)
继承starter parent
配置继承spring-boot-starter-parent:
1 <parent>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-parent</artifactId>
4 <version>2.1.3.RELEASE</version>
5 <relativePath/> <!-- lookup parent from repository -->
6 </parent>
只需要在这里指定Spring Boot的版本号。如果导入其他的starters,你可以完全省略版本号。
使用这个配置,你还可以通过property覆盖内部的依赖。例如,在pom.xml中升级Spring Data release train。
1 <properties>
2 <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
3 </properties>
可以通过spring-boot-dependencies pom,查看支持的属性列表。
不使用parent POM,配置Spring Boot
可能有人不喜欢继承spring-boot-starter-parent POM。你可能有自己的企业标准parent,或者你可能只是比较喜欢明确声明所有的Maven配置。
如果你不想使用spring-boot-starter-parent,你依然可以通过使用scope=import利用依赖管理的便利:
1 <dependencyManagement>
2 <dependencies>
3 <dependency>
4 <!-- Import dependency management from Spring Boot -->
5 <groupId>org.springframework.boot</groupId>
6 <artifactId>spring-boot-dependencies</artifactId>
7 <version>2.1.3.RELEASE</version>
8 <type>pom</type>
9 <scope>import</scope>
10 </dependency>
11 </dependencies>
12 </dependencyManagement>
这种方式不能使用property的形式覆盖原始的依赖项。要达到同样的效果,需要在dependencyManagement里面的spring-boot-dependencies之前添加依赖的东西。例如,要升级Spring Data release train,pom.xml应该是这样的:
1 <dependencyManagement>
2 <dependencies>
3 <!-- Override Spring Data release train provided by Spring Boot -->
4 <dependency>
5 <groupId>org.springframework.data</groupId>
6 <artifactId>spring-data-releasetrain</artifactId>
7 <version>Fowler-SR2</version>
8 <scope>import</scope>
9 <type>pom</type>
10 </dependency>
11 <dependency>
12 <groupId>org.springframework.boot</groupId>
13 <artifactId>spring-boot-dependencies</artifactId>
14 <version>2.1.3.RELEASE</version>
15 <type>pom</type>
16 <scope>import</scope>
17 </dependency>
18 </dependencies>
19 </dependencyManagement>