maven里的依赖什么情况下可以不写版本号

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.1.9.RELEASE</version>
    </parent>

这时候可以不写版本号。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven 中,依赖可以不指定版本时有以下两种情况: 1. 继承自父 POM:如果项目继承了一个父 POM,而该父 POM 中已经声明了某个依赖项的版本号,则在子项目中可以不指定该依赖项的版本号。子项目会自动继承父 POM 中的版本号。例如,以下是一个父 POM 的示例: ```xml <project> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.8</version> </dependency> </dependencies> </project> ``` 在子项目中,我们可以这样声明依赖项,而不需要指定版本号: ```xml <project> <parent> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>1.0.0</version> </parent> <groupId>com.example</groupId> <artifactId>child</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> </dependencies> </project> ``` 2. 使用 Maven BOM:Maven BOM(Bill of Materials)是一种特殊的 POM 文件,它仅包含依赖项的声明,而不包含任何插件或构建配置。通过在子项目中引用 Maven BOM,我们可以统一管理多个依赖项的版本号。在这种情况下,我们可以在依赖项声明中不指定版本号。例如,以下是一个 Maven BOM 的示例: ```xml <project> <groupId>com.example</groupId> <artifactId>my-bom</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>5.3.8</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </project> ``` 在子项目中,我们可以这样引用 Maven BOM,并声明依赖项,而不需要指定版本号: ```xml <project> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <scope>compile</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-bom</artifactId> <version>1.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> ``` 总之,如果某个依赖项的版本号已经在项目中的父 POM 或 Maven BOM 中被声明了,那么在子项目中可以不指定该依赖项的版本号

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值