maven-dependency与dependencyManagement的区别

一般在项目的最顶层pom中才会看到dependencyManagement元素。
使用pom.xml 中的dependencyManagement元素能让所有在子项目中引用一个依赖而不用显式的列出版本号。Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用这个dependencyManagement元素中指定的版本号。

  • 父工程创建好之后应该先mvn:install maven仓库,方便子项目使用

  • 统一管理,方便升级

<!-- 子模块继承之后,提供作用:锁定版本 + 子modlue不用写groupId和version -->
<dependencyManagement>
    <dependencies>
      <!-- spring boot 2.2.2 -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.2.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- spring cloud Hoxton.SR1-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring cloud alibaba 2.1.0-->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.1.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.version}</version>
      </dependency>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  • dependencyManagement只是声明依赖,不会引入依赖,主要作用是:管理项目依赖
  • 如果dependencies里的dependency没有version元素,会寻找dependencyManagement标签里的依赖申明
  • 如果dependencies里的dependency有version元素,就使用dependency中的version元素
  • 子项目会全部继承夫项目中的dependencies元素
  • dependencyManagement只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项
posted @ 2022-02-17 15:25  生生灯火半杯月  阅读(96)  评论(0)    收藏  举报