spring-boot-dependencies、spring-boot-starter-parent、io.spring.platform详解
上一篇文章介绍了 springboot 依赖版本号管理的几种方式,现在来详细分析一下 spring-boot-dependencies、spring-boot-starter-parent、io.spring.platform 是如何进行版本号控制的,各自有什么作用和区别。
一、spring-boot-dependencies、spring-boot-starter-parent、io.spring.platform 三者是继承关系
1.spring-boot-starter-parent 继承 spring-boot-dependencies
2.io.spring.platform 继承 spring-boot-starter-parent
二、spring-boot-dependencies
从继承的源点 spring-boot-dependencies 开始看
1.pom.xml 里的 dependencyManagement 节点
dependencyManagement 节点的作用是统一 maven 引入依赖 JAR 包的版本号,可以看出 spring-boot-dependencies 最重要的一个作用就是对 springboot 可能用到的依赖 JAR 包做了版本号的控制管理
2.pom.xml 里的 pluginManagement 节点
pluginManagement 节点的作用是统一 maven 引入插件的版本号,可以看出 spring-boot-dependencies 另一个作用是对 springboot 可能用到的插件做了版本号的控制管理
3.pom.xml 里的 plugins 节点
spring-boot-dependencies 引入(或覆盖)了三个插件:
maven-help-plugin:用于获取有关项目或系统的帮助信息;这个插件是 Maven 自带的插件,这里进行了覆盖设置;设置 inherited(是否继承)为 false;设置 phase 为 generate-resources、goal 为 effective-pom 的配置 output
<plugin> <artifactId>maven-help-plugin</artifactId> <inherited>false</inherited> <executions> <execution> <id>generate-effective-dependencies-pom</id> <phase>generate-resources</phase> <goals> <goal>effective-pom</goal> </goals> <configuration> <output>${project.build.directory}/effective-pom/spring-boot-dependencies.xml</output> </configuration> </execution> </executions> </plugin>
xml-maven-plugin:处理 XML 相关
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>xml-maven-plugin</artifactId> <version>1.0</version> <inherited>false</inherited> <executions> <execution> <goals> <goal>transform</goal> </goals> </execution> </executions> <configuration> <transformationSets> <transformationSet> <dir>${project.build.directory}/effective-pom</dir> <stylesheet>src/main/xslt/single-project.xsl</stylesheet> <outputDir>${project.build.directory}/effective-pom</outputDir> </transformationSet> </transformationSets> </configuration> </plugin>
build-helper-maven-plugin:用于设置主源码目录、测试源码目录、主资源文件目录、测试资源文件目录等
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <inherited>false</inherited> <executions> <execution> <id>attach-artifacts</id> <phase>package</phase> <goals> <goal>attach-artifact</goal> </goals> <configuration> <artifacts> <artifact> <file>${project.build.directory}/effective-pom/spring-boot-dependencies.xml</file> <type>effective-pom</type> </artifact> </artifacts> </configuration> </execution> </executions> </plugin>
这三个插件共同完成了一件事,将 spring-boot-dependencies(springboot 在项目里使用到的依赖)输出到 XML,并且打包 install 到仓库。
这些就是 spring-boot-dependencies 主要的作用了,管理控制依赖版本号,管理控制插件版本号以及引入了 3 个辅助插件。
三、spring-boot-starter-parent
spring-boot-starter-parent 继承 spring-boot-dependencies
1.pom.xml 里的 properties 节点
spring-boot-starter-parent 在 properties 节点里添加了一些预设配置
java.version:jdk 的版本号
<java.version>1.6</java.version>
resource.delimiter:设定占位符为 @
<resource.delimiter>@</resource.delimiter>
project.build.sourceEncoding、project.reporting.outputEncoding:设置编码为 UTF-8
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
maven.compiler.source、maven.compiler.target:设置编译打包的 jdk 版本
<maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target>
2.pom.xml 里的 dependencyManagement 节点
覆盖了 spring-boot-dependencies 的 spring-core 依赖引入,去掉了 spring-core 里的 commons-logging 依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>
3.pom.xml 里的 bulid->resources 节点
设置了 application.properties 配置文件的读取目录在 /src/main/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>
4.pom.xml 里的 pluginManagement 节点
覆盖了 spring-boot-dependencies 的一些插件版本控制管理:maven-failsafe-plugin、maven-jar-plugin、maven-surefire-plugin、maven-war-plugin、exec-maven-plugin、maven-resources-plugin、git-commit-id-plugin、spring-boot-maven-plugin、maven-shade-plugin
maven-failsafe-plugin:配置了绑定 Maven 打包时 integration-test、verify 阶段
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin>
maven-jar-plugin:添加了启动类配置和扫描默认实现 JAR 包配置
<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>
maven-surefire-plugin:配置了 Maven 打包时单元测试扫描 **/*Tests.java、**/*Test.java 类,排除 **/Abstract*.java 类
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <includes> <include>**/*Tests.java</include> <include>**/*Test.java</include> </includes> <excludes> <exclude>**/Abstract*.java</exclude> </excludes> </configuration> </plugin>
maven-war-plugin:配置了可以没有 web.xml 文件进行启动;添加了启动类配置和扫描默认实现 JAR 包配置
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <archive> <manifest> <mainClass>${start-class}</mainClass> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin>
exec-maven-plugin:添加了启动类配置
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <configuration> <mainClass>${start-class}</mainClass> </configuration> </plugin>
maven-resources-plugin:配置了资源占位符为 @
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <delimiters> <delimiter>${resource.delimiter}</delimiter> </delimiters> <useDefaultDelimiters>false</useDefaultDelimiters> </configuration> </plugin>
git-commit-id-plugin:配置了绑定 Maven 打包修订阶段 revision 的 GIT 版本号变化;配了 verbose 为 ture;配置了日期格式为 yyyy-MM-dd'T'HH🇲🇲ssZ;配置了生成 GIT .properties 文件,文件名为 ${project.build.outputDirectory}/git.properties
<plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> <executions> <execution> <goals> <goal>revision</goal> </goals> </execution> </executions> <configuration> <verbose>true</verbose> <dateFormat>yyyy-MM-dd'T'HH🇲🇲ssZ</dateFormat> <generateGitPropertiesFile>true</generateGitPropertiesFile> <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename> </configuration> </plugin>
spring-boot-maven-plugin:配置了绑定 Maven 打包 repackage 阶段插件的使用;配置了启动类
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> <configuration> <mainClass>${start-class}</mainClass> </configuration> </plugin>
maven-shade-plugin:覆盖引入 spring-boot-maven-plugin 依赖 JAR;配置 keepDependenciesWithProvidedScope 为 true;配置 createDependencyReducedPom 为 true;过滤掉 META-INF/*.SF、META-INF/*.DSA、META-INF/*.RSA,防止重复引用打包失败;配置绑定 Maven 打包 package 阶段 shade;
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.4.RELEASE</version> </dependency> </dependencies> <configuration> <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope> <createDependencyReducedPom>true</createDependencyReducedPom> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer"> <resource>META-INF/spring.factories</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>${start-class}</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
四、io.spring.platform
io.spring.platform 继承 spring-boot-starter-parent
1.pom.xml 里的 properties 节点
io.spring.platform 一个最大的作用便是将经过集成测试的各类依赖版本号进行整合。
在平时开发中,需要某个 JAR 包依赖往往是习惯性的找最新版本,或是根据经验选择一个版本;
单对某个 JAR 包来讲,没有任何问题,但当过多的 JAR 包依赖整合到一起的时候,就可能会出现各自版本不适配的情况产生,产生 BUG 漏洞的场景将大大增加;
io.spring.platform 所做的事情就是将做过集成测试 JAR 包依赖整合到一起,大大降低了漏洞出现的可能性。
2.pom.xml 里的 dependencyManagement 节点
覆盖所有父节点里的依赖引入并增加部分新的依赖,使用 properties 节点里的版本号
3.pom.xml 里的 plugins 节点
覆盖 spring-boot-dependencies 的插件:maven-help-plugin
maven-help-plugin:
<plugin> <artifactId>maven-help-plugin</artifactId> <executions> <execution> <id>generate-effective-dependencies-pom</id> <phase>generate-resources</phase> <goals> <goal>effective-pom</goal> </goals> <configuration> <output>${project.build.directory}/effective-pom.xml</output> </configuration> </execution> </executions> <inherited>false</inherited> </plugin>
新增的插件:gmavenplus-plugin、build-helper-maven-plugin
gmavenplus-plugin:
<plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.1</version> <executions> <execution> <goals> <goal>execute</goal> </goals> <phase>test</phase> </execution> </executions> <configuration> <scripts> <script>file:///${project.basedir}/src/main/groovyScripts/generateBomPropertiesFile.groovy</script> </scripts> </configuration> <dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.3.0</version> </dependency> </dependencies> <inherited>false</inherited> </plugin>
build-helper-maven-plugin:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <executions> <execution> <id>attach-artifacts</id> <phase>package</phase> <goals> <goal>attach-artifact</goal> </goals> <configuration> <artifacts> <artifact> <file>${project.build.directory}/platform-bom.properties</file> <type>properties</type> </artifact> </artifacts> </configuration> </execution> </executions> <inherited>false</inherited> </plugin>
这样分析之后,对 spring-boot-dependencies、spring-boot-starter-parent、io.spring.platform 就有了基本的了解了。
这三者最主要的作用还是管理各类依赖的版本控制。
我们完全可以自己做一个 pom 来管理 springboot 的依赖引入版本管理,后续文章会展示。
对于 pom 里各类插件引入的作用,后续也会详细分析。
最终的目的是做到知其然,知其所以然,这样开发起来才会有大局观。