上一篇文章介紹了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:mm: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:mm: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里各類插件引入的作用,后續也會詳細分析。
最終的目的是做到知其然,知其所以然,這樣開發起來才會有大局觀。