在 SpringBoot 開發(fā)時,我們常常會發(fā)現一個現象:即在 pom 文件中,加入一個新的依賴,往往不需要引入相應的版本號(如下代碼塊所示),就可以正常引入依賴,這其實是因為我們依賴了 spring-boot-starter-parent 模塊的緣故!
點 spring-boot-starter-parent 進去查看源文件會發(fā)現,spring-boot-starter-parent 繼承了 spring-boot-dependencies!
點 spring-boot-dependencies 進去查看源文件會發(fā)現,它在管理著相關依賴的版本。在 dependencyManagement 進行依賴管理,在 pluginManagement 中進行插件管理!
~
本篇內容包括:spring-boot-dependencies 模塊介紹、對 spring-boot-dependencies 依賴管理方法的借鑒
一、spring-boot-dependencies 模塊介紹
1、關于 spring-boot-starter-parent 模塊
在 SpringBoot 開發(fā)時,我們常常會發(fā)現一個現象:即在 pom 文件中,加入一個新的依賴,往往不需要引入相應的版本號(如下代碼塊所示),就可以正常引入依賴,這其實是因為我們依賴了 spring-boot-starter-parent 模塊的緣故!
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 在這里指定了 spring boot 的版本 -->
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<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>
...
</dependencies>
2、關于 spring-boot-dependencies 模塊
點 spring-boot-starter-parent 進去查看源文件會發(fā)現,spring-boot-starter-parent 繼承了 spring-boot-dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.2</version>
</parent>
點 spring-boot-dependencies 進去查看源文件會發(fā)現,它在管理著相關依賴的版本。在 dependencyManagement 進行依賴管理,在 pluginManagement 中進行插件管理:
二、對 spring-boot-dependencies 依賴管理方法的借鑒
1、pom.xml 里的 dependencyManagement 節(jié)點
dependencyManagement 節(jié)點的作用是統一 maven 引入依賴 Jar 包的版本號,可以看出 spring-boot-dependencies 最重要的一個作用就是對 springboot 可能用到的依賴 Jar 包做了版本號的控制管理。
2、pom.xml 里的 pluginManagement 節(jié)點
pluginManagement 節(jié)點的作用是統一 Maven 引入插件的版本號,可以看出 spring-boot-dependencies 另一個作用是對 SpringBoot 可能用到的插件做了版本號的控制管理。
3、pom.xml 里的 plugins 節(jié)點
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 個輔助插件。
4、對 spring-boot-dependencies 依賴管理方法的借鑒
spring-boot-dependencies 對依賴的管理方法,我們也可以借鑒一下。
spring-boot-dependencies 只管理著部分依賴,還有一些第三方依賴沒有管理到,當我們創(chuàng)建微服務時,就可以使用這種方法來管理父類的 POM 文件,把依賴的版本號集中在主POM中管理,其他子項目只需要在使用的時候引入即可,無需寫版本號。
單項目,那就沒有這么管理的必要了,不要為了管理而管理。文章來源:http://www.zghlxwxcb.cn/news/detail-531420.html
總結一下,spring-boot-dependencies 對插件的管理方法為:文章來源地址http://www.zghlxwxcb.cn/news/detail-531420.html
<!--版本號管理 start -->
<properties>
<test.version>5.5.2</test.version>
<plugin.version>5.5.2</plugin.version>
</properties>
<!--版本號管理 end -->
<!-- 依賴管理 start -->
<dependencyManagement>
<dependencies>
<groupId>xxxx</groupId>
<artifactId>xx-maven-plugin</artifactId>
<version>${test.version}</version>
</dependencies>
</dependencyManagement>
<!-- 依賴管理 end-->
<!-- 插件管理 end-->
<pluginManagement>
<plugins>
<groupId>xxxx</groupId>
<artifactId>xxxxxxx</artifactId>
<version>${plugin.version}</version>
</plugins>
</pluginManagement>
<!-- 插件管理 end-->
到了這里,關于SpringBoot:模塊探究之spring-boot-dependencies的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!