在使用spring boot时,parent都需要继承自 spring-boot-starter-parent,查看官方的示例也是继承自这个parent,但是有时候我们有自己定义的parent项目,在这种情况下,我们该如何达到既使用了我们自定义的parent,又能将spring-boot集成进来呢?
1、官方示例-继承spring-boot-starter-parent
在spring boot官方示例中,springboot项目继承自 spring-boot-starter-parent,pom.xml文件如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
这个parent引用的作用如下:
1.1 统一定义配置
spring-boot-starter-parent部分代码如下,定义编码、java版本等:
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
1.2 统一依赖及版本
spring-boot-starter-parent部分代码如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
spring-boot-dependencies代码如下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencyManagement>
可以看到在 <dependencyManagement>
中定义了非常多指定版本的依赖,比如上述的redis、quartz等,这时我们在具体开发的模块中只需按以下方式引入依赖即可,而无需指定版本号:
<!-- redis 缓存操作 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 定时任务 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
所以spring-boot-starter-parent具体作用如下:
- 引入父pom里面的依赖时无须指定版本;
- java版本,项目编码格式,资源引用描述符已经设置好
- 插件管理
a.封装了配置文件的过滤规则
b.封装了打可执行jar、war的配置
c.封装了插件的版本信息
d.封装了日期格式
e.引入了eclipse和IDEA相关依赖简化了配置,达到开箱即用等
2、使用自定义parent方案
如果我们需要使用自定义的parent时,就无法使用 spring-boot-starter-parent,在这种情况下,我们该如何达到既使用了我们自定义的parent,又能将spring-boot集成进来呢?
springboot已经给了我们解决方案(替代方案)。根据上文可知,spring-boot-starter-parent的主要作用,完全可以有 dependencies 来替代,在父类的pom文件中设置 dependencies 并制定相关版本号。步骤如下:
-
删掉默认继承的spring-boot-starter-parent 这个parent
-
添加如下声明:
在我们自己 parent 项目中,加入下面的声明:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注意,该 dependency 的 type 是 pom,scope 是 import,这种类型的 dependency 只能在 dependencyManagement 标签中声明。
- 添加自己的parent
在我们项目中的子项目中,parent的声明修改为我们自己项目的 parent 项目就可以了,比如,我的是:
<parent>
<groupId>org.test</groupId>
<artifactId>spring</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
这里有点需要注意,在子项目的 dependencies 中,不需要(也不能)再次添加对 spring-boot-dependencies 的声明了,否则子项目将无法编译通过。即在子项目中,下面的配置是多余的:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
</dependency>
- 修改spring-boot-maven-plugin为如下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
参考文档:spring 官方文档
评论区