前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot:模块探究之spring-boot-dependencies

SpringBoot:模块探究之spring-boot-dependencies

作者头像
栗筝i
发布2022-12-23 08:44:06
3K0
发布2022-12-23 08:44:06
举报
文章被收录于专栏:迁移内容迁移内容

在 SpringBoot 开发时,我们常常会发现一个现象:即在 pom 文件中,加入一个新的依赖,往往不需要引入相应的版本号(如下代码块所示),就可以正常引入依赖,这其实是因为我们依赖了 spring-boot-starter-parent 模块的缘故! 点 spring-boot-starter-parent 进去查看源文件会发现,spring-boot-starter-parent 继承了 spring-boot-dependencies! 点 spring-boot-dependencies 进去查看源文件会发现,它在管理着相关依赖的版本。在 dependencyManagement 进行依赖管理,在 pluginManagement 中进行插件管理! ~ 本篇内容包括:spring-boot-dependencies 模块介绍、对 spring-boot-dependencies 依赖管理方法的借鉴


文章目录


一、spring-boot-dependencies 模块介绍

1、关于 spring-boot-starter-parent 模块

在 SpringBoot 开发时,我们常常会发现一个现象:即在 pom 文件中,加入一个新的依赖,往往不需要引入相应的版本号(如下代码块所示),就可以正常引入依赖,这其实是因为我们依赖了 spring-boot-starter-parent 模块的缘故!

代码语言:javascript
复制
<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 进去查看源文件会发现,spring-boot-starter-parent 继承了 spring-boot-dependencies

代码语言:javascript
复制
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.7.2</version>
  </parent>

点 spring-boot-dependencies 进去查看源文件会发现,它在管理着相关依赖的版本。在 dependencyManagement 进行依赖管理,在 pluginManagement 中进行插件管理:

image-20221219105152638
image-20221219105152638

二、对 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

代码语言:javascript
复制
<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相关

代码语言:javascript
复制
<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:用于设置主源码目录、测试源码目录、主资源文件目录、测试资源文件目录等

代码语言:javascript
复制
<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 只管理着部分依赖,还有一些第三方依赖没有管理到,当我们创建微服务时,就可以使用这种方法来管理父类的 POM 文件,把依赖的版本号集中在主POM中管理,其他子项目只需要在使用的时候引入即可,无需写版本号。

单项目,那就没有这么管理的必要了,不要为了管理而管理。

总结一下,spring-boot-dependencies 对插件的管理方法为:

代码语言:javascript
复制
<!--版本号管理 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-->
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-12-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、spring-boot-dependencies 模块介绍
    • 1、关于 spring-boot-starter-parent 模块
      • 2、关于 spring-boot-dependencies 模块
      • 二、对 spring-boot-dependencies 依赖管理方法的借鉴
        • 1、pom.xml 里的 dependencyManagement 节点
          • 2、pom.xml 里的 pluginManagement 节点
            • 3、pom.xml 里的 plugins 节点
              • 4、对 spring-boot-dependencies 依赖管理方法的借鉴
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档