目录
一、前言
在idea当中创建springboot项目的时候都会继承一个spring-boot-starter-parent
作为父类,假如不继承我们的项目就不能使用了吗?他的作用是什么呢?报着这些疑问我们进行深度解析。

二、Maven继承
Maven 在设计时,借鉴了 Java 面向对象中的继承思想,提出了 POM 继承思想。当一个项目包含多个模块时,可以在该项目中再创建一个父模块,并在其 POM 中声明依赖,其他模块的 POM 可通过继承父模块的 POM 来获得对相关依赖的声明
。对于父模块而言,其目的是为了消除子模块 POM 中的重复配置,其中不包含有任何实际代码,因此父模块 POM 的打包类型(packaging)必须是 pom
。
通过mvn help:effective-pom 命令就可以查看项目的最终生成的pom(有效的pom)。mvn help:effective-pom命令详解:https://blog.csdn.net/weixin_43888891/article/details/130483451
三、分析spring-boot-starter-parent
不继承我们的项目就不能使用了吗?
(1)了解spring-boot-starter-parent

spring-boot-starter-parent下大部门都是pluginManagement插件管理。

(2)了解spring-boot-dependencies
接下来我们再点进去spring-boot-dependencies看看,spring-boot-dependencies给我们提供了大量的dependencyManagement依赖版本管理。
Maven 可以通过 dependencyManagement 元素对依赖进行管理,它具有以下 2 大特性:
- 在该元素下声明的依赖不会实际引入到模块中,只有在 dependencies 元素下同样声明了该依赖,才会引入到模块中。
- 该元素能够约束 dependencies 下依赖的使用,即 dependencies 声明的依赖若未指定版本,则使用 dependencyManagement 中指定的版本,否则将覆盖 dependencyManagement 中的版本。

(3)不引用spring-boot-starter-parent项目如何正常使用
新建一个空项目,只引入web(注意没有引入boot版本管理,那就需要手动添加版本号),springboot照样可以启动的哟

四、Maven单继承问题
现在有个问题,我现在想使用spring-boot-starter-parent
提供的依赖管理,但是我又不想继承他,因为我还要继承别的项目,这时候该怎么办呢?
官网讲解:https://docs.spring.io/spring-boot/docs/3.1.0-SNAPSHOT/maven-plugin/reference/htmlsingle/#using.import

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
类似于
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/>
</parent>
只是类似,并不完全替代继承。为什么这么说?请看如下示例:spring-boot-starter-parent的pluginManagement是有对spring-boot-maven-plugin版本进行管理的:

找不到说明一个原因,导入的配置没有生效!


说明使用dependencyManagement来替代parent的时候,pluginManagement里面嵌套的plugins版本并没有继承过来。
注:import 依赖范围只能与 dependencyManagement 元素配合使用才会有效,其功能是将目标 pom.xml 中的 dependencyManagement 配置导入合并到当前 pom.xml 的 dependencyManagement 中。
五、不继承spring-boot-starter-parent需要注意的
假如不继承spring-boot-starter-parent,我们还需要自己声明打包插件。spring-boot-starter-parent配置的插件就是打出来一个可直接运行的jar。
假如我们只声明如下,打出来的jar包是启动不起来的,打出来的jar包并不会将依赖的jar打进去。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.10</version>
</plugin>
</plugins>
</build>