Maven 中 dependencyManagement 干嘛用的

首先我么都知道Maven是用来管理jar包的,最常见的就是

dependencies,下面有  groupId,artifactId,version 3个属性
  <!--
      导入依赖,最常见的
      groupId,artifactId,version
  -->
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.2.RELEASE</version>
    </dependency>
  </dependencies>

还有一种情况,你可能会碰到,就是没有版本号

dependencies,下面有  groupId,artifactId, 2个属性
  <!--
      只有,没有版本号
      groupId,artifactId
  -->
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>
  </dependencies>

</project>

那么问题来了,没有版本号,那么Maven怎么知道下载哪个版本?

那么Maven肯定不会下载  spring-context 对应的jar包,在Maven下Dependencis会报红色的波浪线。


我们来看看 dependencyManagement 
首先要了解:  dependencyManagement   和  dependencies 区别
dependencies :会从仓库下载指定的jar包
dependencyManagement : 不会下载jar包,就是个描述,声明,不会从仓库下载

maven怎么知道下载哪个版本,有2种途径:
 
 1. 就是你在 dependencies 指定 <version>
2. 当 dependencies 没有 <version> ,就去 dependencyManagement 查找指定的 <version>

注意:如果dependencies中的dependency声明了version,那么无论dependencyManagement中有无对该jar的version声明,
都以dependency里的version为准
<!--指定版本号-->
  <dependencyManagement>
     <dependencies>
       <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>5.5.2.RELEASE</version>
       </dependency>
     </dependencies>
  </dependencyManagement>

  <!--
      只有,没有版本号 groupId,artifactId
      到  <dependencyManagement> 去找
  -->
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>
  </dependencies>
 
原文地址:https://www.cnblogs.com/codelives/p/13220982.html