场景
首先,我们需要明白的是,使用这2个jar的目的,是为了解决什么问题呢?
当我们使用 spring 或 spring-boot 开发项目时,需要引入很多依赖,包括 spring 本身的组件、各种 spring-boot-starter、以及其它第三方依赖(如:slf4j、redis)。
依赖多了,版本的选择是个问题,就怕哪个版本选择的不对导致出现一些意想不到的 BUG。
为了解决这些问题,所以引入了版本依赖管理。
区别
spring-boot-starter-parent 和 spring-boot-dependencies,都是为了解决版本依赖管理的。
但是spring-boot-starter-parent 的功能更加强一点,除了管理依赖的版本号,还规定了UTF-8编码格式、配置过滤替换、插件配置、规范占位符 等等。
下面,我们来看下spring-boot-starter-parent :
可以看到,其实spring-boot-dependencies是spring-boot-starter-parent的parent。
而spring-boot-dependencies其实是一个pom工程,主要作用是,确定依赖的版本号,主要是通过properties
和 dependencyManagement
这2个标签来实现的。如下图:
接下来,视线再转到spring-boot-starter-parent。
maven-resources-plugin插件
这个maven-resources-plugin插件,确定了将哪些资源文件,打包到包中。
这个maven-resources-plugin用到了以下的properties 和 resouce:
<properties>
<resource.delimiter>@</resource.delimiter>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
</resources>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<propertiesEncoding>${project.build.sourceEncoding}</propertiesEncoding>
<delimiters>
<delimiter>${resource.delimiter}</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
maven-jar-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${start-class}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
spring-boot-maven-plugin
refer to : https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#introduction
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
maven-compiler-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
实际使用
在项目中,我们可以将spring-boot-starter-parent作为我们项目的parent,直接进行继承。通过继承spring-boot-starter-parent,来实现对依赖的版本号管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jshxhb</groupId>
<artifactId>iot2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>iot2</name>
如果,我们已经有一个parent了,那么就不能继承spring-boot-starter-parent,那么此时可以将spring-boot-dependencies将入到dependencyManagement中即可。
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>