• 使用spring-boot-dependencie进行项目版本管理


    在初学spring boot时,官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent 这个parent:

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>1.5.1.RELEASE</version>
    5. </parent>
    6. <dependencies>
    7. <dependency>
    8. <groupId>org.springframework.boot</groupId>
    9. <artifactId>spring-boot-starter-web</artifactId>
    10. </dependency>
    11. </dependencies>

    但是,一般情况下,在我们自己的项目中,会定义一下自己的 parent 项目,这种情况下,上面的这种做法就行不通了。那么,该如何来做呢?其实,在spring的官网也给出了变通的方法的:

    在我们自己 parent 项目中,加下下面的声明

    1. <dependencyManagement>
    2. <dependencies>
    3. <dependency>
    4. <groupId>org.springframework.boot</groupId>
    5. <artifactId>spring-boot-dependencies</artifactId>
    6. <version>1.5.1.RELEASE</version>
    7. <type>pom</type>
    8. <scope>import</scope>
    9. </dependency>
    10. </dependencies>
    11. </dependencyManagement>

    请注意,它的 type 是 pomscope 是 import,这种类型的 dependency 只能在 dependencyManagement 标签中声明。

    然后,把我们项目中的 子项目 中,parent 的声明,修改为我们自己项目的 parent 项目就可以了,比如,我的是:

    1. <parent>
    2. <groupId>org.test</groupId>
    3. <artifactId>spring</artifactId>
    4. <version>0.1-SNAPSHOT</version>
    5. </parent>

    有一点,需要注意一下。
    在子项目 的 dependencies 中,不需要(也不能)再次添加对 spring-boot-dependencies 的声明了,否则子项目 将无法编译通过。在子项目中,下面的配置是多余的:

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-dependencies</artifactId>
    4. </dependency>

    为什么会这个样子呢?
    因为 spring-boot-dependencies 根本就没有对应的jar包,它只是一个pom 配置,可以去 maven仓库看一下。
    它里面定义了 非常多的依赖声明。

    所以,有了它之后,我们在子项目中使用到的相关依赖,就不需要声明version了,如

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-web</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.springframework.boot</groupId>
    8. <artifactId>spring-boot-starter-test</artifactId>
    9. <scope>test</scope>
    10. </dependency>
    11. </dependencies>
  • 相关阅读:
    【区块链 | IPFS】IPFS cluster私有网络集群搭建
    asp毕业设计——基于asp+sqlserver的WEB社区论坛设计与实现(毕业论文+程序源码)——社区论坛
    搜维尔科技:【第三期】第九届元宇宙数字人大赛,参赛小组报名确认公告
    区块链技术在供应链管理中的创新应用
    vue2技能树(2)-模板语法、vue的工具链、渐进式框架
    ChatGPT新手指南:如何应用于学术论文撰写
    Elasticsearch7教程(3) 查询文档 term terms terms_set
    GLTF格式解析
    《互联网的世界》第四讲-拥塞控制与编码
    TDesign的input标签
  • 原文地址:https://blog.csdn.net/bbj12345678/article/details/125633659