广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >SpringBoot:模块探究之spring-boot-dependencies
  • 279
分享到

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

springspringbootjava 2023-09-17 13:09:35 279人浏览 薄情痞子
摘要

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

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 模块的缘故!

<parent>    <groupId>org.springframework.bootgroupId>    <artifactId>spring-boot-starter-parentartifactId>        <version>2.7.2version>    <relativePath/> parent><dependencies>    <dependency>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-WEBartifactId>    dependency>    <dependency>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-testartifactId>        <scope>testscope>    dependency>  ...dependencies>

2、关于 spring-boot-dependencies 模块

点 spring-boot-starter-parent 进去查看源文件会发现,spring-boot-starter-parent 继承了 spring-boot-dependencies

  <parent>    <groupId>org.springframework.bootgroupId>    <artifactId>spring-boot-dependenciesartifactId>    <version>2.7.2version>  parent>

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

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

<plugin>  <artifactId>maven-help-pluginartifactId>  <inherited>falseinherited>  <executions>    <execution>      <id>generate-effective-dependencies-pomid>      <phase>generate-resourcesphase>      <goals>        <goal>effective-pomgoal>      goals>      <configuration>        <output>${project.build.directory}/effective-pom/spring-boot-dependencies.xmloutput>      configuration>    execution>  executions>plugin>

# xml-maven-plugin:处理XML相关

<plugin>  <groupId>org.codehaus.mojogroupId>  <artifactId>xml-maven-pluginartifactId>  <version>1.0version>  <inherited>falseinherited>  <executions>    <execution>      <goals>        <goal>transfORMgoal>      goals>    execution>  executions>  <configuration>    <transformationSets>      <transformationSet>        <dir>${project.build.directory}/effective-pomdir>        <stylesheet>src/main/xslt/single-project.xslstylesheet>        <outputDir>${project.build.directory}/effective-pomoutputDir>      transformationSet>    transformationSets>  configuration>plugin>

# build-helper-maven-plugin:用于设置主源码目录、测试源码目录、主资源文件目录、测试资源文件目录等

<plugin>  <groupId>org.codehaus.mojogroupId>  <artifactId>build-helper-maven-pluginartifactId>  <inherited>falseinherited>  <executions>    <execution>      <id>attach-artifactsid>      <phase>packagephase>      <goals>        <goal>attach-artifactgoal>      goals>      <configuration>        <artifacts>          <artifact>            <file>${project.build.directory}/effective-pom/spring-boot-dependencies.xmlfile>            <type>effective-pomtype>          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 对插件的管理方法为:

<properties><test.version>5.5.2test.version><plugin.version>5.5.2plugin.version>properties><dependencyManagement><dependencies><groupId>xxxxgroupId><artifactId>xx-maven-pluginartifactId><version>${test.version}version>dependencies>dependencyManagement><pluginManagement><plugins><groupId>xxxxgroupId><artifactId>xxxxxxxartifactId><version>${plugin.version}version>plugins>pluginManagement>

来源地址:https://blog.csdn.net/weixin_45187434/article/details/128395040

--结束END--

本文标题: SpringBoot:模块探究之spring-boot-dependencies

本文链接: https://www.lsjlt.com/news/411075.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作