Maven中依赖管理配置的基础理解

一、继承方式(<parent/>)

父项目A、子项目B。

在子ModuleB 中声明<parent/>标签,将父项目A的座标填入,子项目B可以继承父项目A中的:

  1、dependencies: 项目的依赖配置

  2、dependencyManagement: 项目的依赖管理配置

  3、build: <pluginManagement/>、<plugins/>等

  .......

其中需要注意的是dependencies下的必定会继承,也就是说不需要手动在子项目B的pom中去声明;而要想使用父项目A dependencyManagement下的依赖,则需要在子项目B中的依赖中声明(不需要声明version,但是要声明groupId、artifactId);

<pluginManagement/>、<plugins/>类似于上面这两者的关系。

优点:可以继承父项目的大部分属性:dependencies、dependencyManagement等。

缺点:单继承

二、非继承方式

在项目中的<dependencyManagement/>中可以导入其他pom。例如:

<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>
    </dependencies>
</dependencyManagement>

<type/>和<scope/>固定。

优点:可以导入多个pom。

缺点:只能引用所导入pom的<dependencyManagement/>中的依赖。例如:项目A的<dependencyManagement/>中导入了spring-boot-dependencies的pom,那么项目A的子项目B只能引用spring-boot-dependencies中的<dependencyManagement/>部分,其他部分无法引用。

以上纯属个人见解,如有不对,感谢指出!共同进步。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章