likes
comments
collection
share

spring-boot-starter-parent是什么?怎么换用自己的parent?

作者站长头像
站长
· 阅读数 7

在创建一个Spring Boot工程时,我们会发现pom.xml中继承了Spring Boot的一个parent如下:

spring-boot-starter-parent是什么?怎么换用自己的parent?

所以这个parent有什么作用呢?以及如果我们的项目,需要继承其它的或者自己项目中的parent怎么办呢?

在此,我们需要先明白这个parent的作用。

1,spring-boot-starter-parent的作用

我们可以点进去看一下这个parentpom.xml的源码:

spring-boot-starter-parent是什么?怎么换用自己的parent?

折叠全部标签后如下:

spring-boot-starter-parent是什么?怎么换用自己的parent?

可见这个parent没有引入任何依赖,主要是定义了一些属性和环境信息,以及继承了一个spring-boot-dependenciesparent,我们继续点进spring-boot-dependencies看一看:

spring-boot-starter-parent是什么?怎么换用自己的parent?

折叠之后:

spring-boot-starter-parent是什么?怎么换用自己的parent?

可见这个spring-boot-dependencies中也没有引入任何依赖,但是定义了许多依赖的版本,并且使用dependencyManagementpluginManagement进行了依赖和插件的版本管理。

相信现在大家明白怎么回事了,这个spring-boot-starter-parent的作用如下:

  • 定义了一系列属性properties,例如使用的Java版本,使用UTF-8编码构建,资源占位符等等
  • 继承了spring-boot-dependencies这使得我们使用Spring Boot相关组件和插件时都不需要声明版本了,因为继承的spring-boot-dependencies中依赖管理已经定义了使用的版本,我们相关依赖会直接从其中继承

可见之所以我们平时引入许多Spring Boot相关依赖例如Spring Web的Starter时都不需要声明版本,是因为我们继承了spring-boot-starter-parent,这些依赖的版本也会从中继承,非常方便。

spring-boot-starter-parent是什么?怎么换用自己的parent?

2,换用自己的parent

如果说需要继承自己的项目作为parent,那么应当如何修改pom.xml使得我们引入Spring Boot相关组件时,仍然不需要手动管理依赖版本呢?事实上,借助dependencyManagement即可。

我们在项目中加入如下的dependencyManagement节点,引入Spring Boot依赖版本管理:

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>3.2.2</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

加入了这个依赖管理,我们就可以将spring-boot-dependencies中所有的dependencyManagement信息也引入到我们自己的项目中来,这样使用Spring Boot相关依赖,也不需要自己声明版本了,而是可以像之前一样从中继承版本,如需修改Spring Boot版本也在这里面修改即可。这时,我们就可以删除原有的parent节点换用自己的了!

事实上,在我们使用Spring Cloud或者Spring Cloud Alibaba时,也会看到这个依赖管理节点,它们的作用是一样的。

需要注意的是,由于现在不再使用spring-boot-starter-parent,之前继承的一些关于JDK版本等属性也会丢失,建议自己在项目pom.xml中设定一下properties即可:

<properties>
	<java.version>21</java.version>
	<maven.compiler.source>${java.version}</maven.compiler.source>
	<maven.compiler.target>${java.version}</maven.compiler.target>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

除此之外,我们的插件spring-boot-maven-plugin也需要声明一下版本号,和使用的Spring Boot版本号一致即可:

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<version>3.2.2</version>
	<configuration>
		<excludes>
			<exclude>
				<groupId>org.projectlombok</groupId>
				<artifactId>lombok</artifactId>
			</exclude>
		</excludes>
	</configuration>
</plugin>

事实上,我们可以在properties中声明一个属性变量表示使用的Spring Boot版本号,然后在dependencyManagementplugin中引用该变量作为版本号即可,我们修改pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.gitee.swsk33</groupId>
	<artifactId>exception-handler-demo</artifactId>
	<version>1.0.0</version>
	<name>exception-handler-demo</name>
	<description>exception-handler-demo</description>

	<properties>
		<!-- 省略其它属性... -->
		<!-- 定义一个属性变量作为Spring Boot版本号 -->
		<spring.boot.version>3.2.2</spring.boot.version>
	</properties>

	<!-- 省略dependencies... -->

	<dependencyManagement>
		<dependencies>
			<!-- Spring Boot依赖版本管理 -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<!-- 引用属性变量 -->
				<version>${spring.boot.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<!-- 引用属性变量 -->
				<version>${spring.boot.version}</version>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

这样,如果后面要修改Spring Boot版本就方便多了!直接修改这个属性变量spring.boot.version即可。