依赖start和依赖BOM区别
# 依赖Start-parent和依赖BOM区别
- spring-boot-starter-parent 是一个特殊的 Maven POM,它在内部导入了 spring-boot-dependencies BOM,同时还定义了插件配置、资源过滤等默认设置。当项目继承这个 parent 时,会自动获得 BOM 的依赖管理功能,还能使用其默认配置。
- 只需一行配置,就能同时获得版本管理和默认插件配置和资源过滤。
- spring-boot-dependencies 是一个 BOM(物料清单),它只包含依赖管理,也就是定义了各种依赖的版本。项目可以通过
导入这个 BOM,从而实现版本自动控制。
- 只包含版本管理
- 两者的主要区别
对比项 | 继承 spring-boot-starter-parent | 导入 spring-boot-dependencies |
---|---|---|
配置方式 | 单继承,只能有一个 parent | 可导入多个 BOM,更加灵活 |
功能范围 | 包含 BOM、插件配置、资源过滤等默认设置 | 仅包含依赖版本管理 |
适用场景 | 适用于大多数 Spring Boot 项目 | 适用于无法继承 parent 的项目(如多模块项目) |
版本覆盖方式 | 通过 <properties> 覆盖 BOM 中的版本属性 | 通过 <properties> 覆盖 BOM 中的版本属性 |
# 使用spring-boot-starter-parent构建多模块项目
# 项目介绍
parent-pom (聚合根) ├── module-api (普通模块,不包含主类) ├── module-service (普通模块,不包含主类) └── module-app (启动模块,包含主类)
# 父 POM 的关键配置
<!-- parent-pom/pom.xml -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
</parent>
<modules>
<module>module-api</module>
<module>module-service</module>
<module>module-app</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 无需版本,继承自父 POM -->
</plugin>
</plugins>
</pluginManagement>
</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 主应用模块(包含主类)
<!-- module-app/pom.xml -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 无需版本,继承自 pluginManagement -->
<executions>
<execution>
<goals>
<goal>repackage</goal> <!-- 打包可执行 JAR/WAR -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 普通模块(库模块,不包含主类)
<!-- module-api/pom.xml -->
<build>
<plugins>
<!-- 无需配置 spring-boot-maven-plugin -->
<!-- 普通模块不需要打包为可执行 JAR -->
</plugins>
</build>
1
2
3
4
5
6
7
2
3
4
5
6
7
# 使用spring-boot-dependencies构建多模块项目
# 项目介绍
parent-pom (聚合根) ├── module-api (普通模块,不包含主类) ├── module-service (普通模块,不包含主类) └── module-app (启动模块,包含主类)
# 父 POM 的关键配置
<!-- parent-pom/pom.xml -->
<properties>
<maven.compiler.version>3.11.0</maven.compiler.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<modules>
<module>module-api</module>
<module>module-service</module>
<module>module-app</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<target>${maven.compiler.target}</target>
<source>${maven.compiler.source}</source>
<encoding>UTF-8</encoding>
<parameters>true</parameters>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 主应用模块(包含主类)
<!-- module-app/pom.xml -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 无需版本,继承自 pluginManagement -->
<executions>
<execution>
<goals>
<goal>repackage</goal><!-- 打包可执行 JAR/WAR -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 普通模块(库模块,不包含主类)
<!-- module-api/pom.xml -->
<build>
<plugins>
<!-- 无需配置 spring-boot-maven-plugin -->
<!-- 普通模块不需要打包为可执行 JAR -->
</plugins>
</build>
1
2
3
4
5
6
7
2
3
4
5
6
7