当前位置: 首页 > news >正文

Maven中为什么有些依赖不用引入版本号

先给出一个例子:

<parent>
        <artifactId>sky-take-out</artifactId>
        <groupId>com.sky</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>sky-server</artifactId>
    <dependencies>

        <dependency>
            <groupId>com.sky</groupId>
            <artifactId>sky-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.sky</groupId>
            <artifactId>sky-pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>



        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

    

      
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>


    </dependencies>

每个依赖都没有引入版本号,是因为:

从父 POM 继承了版本号

若项目有父 POM,并且父 POM 里定义了依赖的版本号,子项目就能够直接使用该依赖,无需再指定版本号。像上面给出的 POM 文件,就定义了父项目:

<parent>
    <artifactId>sky-take-out</artifactId>
    <groupId>com.sky</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

而这个父项目也有它的父项目:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.3.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

Spring Boot 的 spring-boot-starter-parent POM 文件包含了 <dependencyManagement> 标签,此标签里定义了一系列常用依赖的版本。当项目继承自 spring-boot-starter-parent 时,就能使用这些预定义的版本,无需再次指定。

例如,在 spring-boot-starter-parent 的 <dependencyManagement> 部分,可能已经定义了 spring-boot-starter-web 的版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.3.4</version>
        </dependency>
        <!-- 其他依赖 -->
    </dependencies>
</dependencyManagement>

可以通过下面的方式查看 spring-boot-starter-parent 的 <dependencyManagement> 内容:

  1. 前往 Maven 中央仓库 搜索 spring-boot-starter-parent,找到对应版本(如 3.3.4)的 POM 文件。
  2. 查看 POM 文件中的 <dependencyManagement> 部分,了解所有预定义的依赖版本。

相关文章:

  • 尝试在软考61天前开始成为软件设计师-数据结构算法
  • 内核编程十:进程的虚拟地址空间
  • Docker Hub Mirror 终极解决方案——0成本,超高速!
  • RAG优化:python从零实现时间管理大师Self-RAG
  • 红宝书第十二讲:详解JavaScript中的工厂模式与原型模式等各种设计模式
  • 第八章:防火墙
  • [实操]Mysql8 InnoDB引擎主从配置三节点操作流程
  • 论文阅读笔记:Denoising Diffusion Implicit Models
  • 消息队列ActiveMQ、RabbitMQ、RocketMQ、Kafka对比分析和选型
  • 【杂谈】Godot4.4 对象池(附脚本)
  • 盖泽 寻边器 帮助类
  • 开发中后端返回下划线数据,要不要统一转驼峰?
  • 【HTML5游戏开发教程】零基础入门合成大西瓜游戏实战 | JS物理引擎+Canvas动画+完整源码详解
  • C# BULK INSERT导入大数据文件数据到SqlServer
  • centos7.9镜像源及Python引入ssl问题处理
  • OLED中英文混合显示
  • 如何设计一个处理物联网设备数据流的后端系统。
  • SpringMVC 配置详解
  • 《深度剖析:DevEco Studio 如何实现人工智能模型的高效可视化开发》
  • 交换机(access端口)
  • 国际著名学者Charles M. Lieber全职受聘清华深圳国际研究生院
  • 49:49白热化,美参议院对新关税政策产生巨大分歧
  • 铁路上海站迎五一假期客流最高峰,今日预计发送77万人次
  • 逛了6个小时的上海车展。有些不太成熟的感受。与你分享。
  • 民生访谈|宝妈宝爸、毕业生、骑手……上海如何为不同人群提供就业保障
  • 持续更新丨伊朗内政部长:港口爆炸已致14人死亡