maven 依赖继承讲解

maven 继承的方式有两种
1. 通过
    <parent>
        <groupId>com.thomas.yang</groupId>
        <artifactId>School</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../School/pom.xml</relativePath>
    </parent>

    这种方式,引用父工程中信息,如果父工程中,有dependencies 不在dependencyManagement 内,通过
    parent 的方式会直接导入导入到子工程,子工程不需要在声明这个引用

    dependencyManagement内中的引用,子类想用使用,必须要在子工程的pom文件中的有dependencies中声明
    引用父类中的那个依赖,不声明,不会引用;

2. 子类通过dependencyManagement 中scope = Import 标签来声明导入那个工程,可以实现多继承;
   子类pom
      <!--通过这个标签可以继承其他类型的工程,从而实现多继承-->
         <dependencyManagement>
             <dependencies>
                 <!-- 此处继承了School 和 School_B 两个项目,type为pom,scope 为 import -->
                 <dependency>
                     <groupId>com.thomas.yang</groupId>
                     <artifactId>School</artifactId>
                     <version>1.0-SNAPSHOT</version>
                     <!--声明依赖类型只能是pom -->
                     <type>pom</type>
                     <scope>import</scope>
                 </dependency>
                 <dependency>
                     <groupId>com.thomas.yang</groupId>
                     <artifactId>School_B</artifactId>
                     <version>1.0-SNAPSHOT</version>
                     <type>pom</type>
                     <scope>import</scope>
                 </dependency>
             </dependencies>
         </dependencyManagement>

   子类虽然在dependencyManagement 中声明了导入依赖,但是导入的依赖的pom文件中可能会有很多其他
   依赖,所以子类中需要用哪些依赖必须在dependencies 中声明如下:
          <dependencies>
               <!-- 从继承的父项目中继承依赖 -->
               <dependency>
                   <groupId>junit</groupId>
                   <artifactId>junit</artifactId>
               </dependency>
               <dependency>
                   <groupId>org.mybatis</groupId>
                   <artifactId>mybatis</artifactId>
               </dependency>
               <dependency>
                   <groupId>mysql</groupId>
                   <artifactId>mysql-connector-java</artifactId>
               </dependency>
           </dependencies>

   综上两种,引用的相同点,是必须在子工程中声明,需要引用哪些依赖
   不同点parent的方式 可以直接依赖到父工程 公共的依赖,不需要在子工程中声明
   dependencyManagement 不能直接依赖到父工程 公共的依赖;

 

测试源码地址:https://gitee.com/Thomas520021/maven_dependency.git

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值