• maven的核心概念——继承


    15.1 为什么需要继承机制

    由于非compile范围的依赖信息是不能在“依赖链”中传递的,所以有需要的工程只能单独配置。例如:

    Hello
    
    <dependency>
    
    <groupId>junit</groupId>
    
    <artifactId>junit</artifactId>
    
    <version>4.9</version>
    
    <scope>test</scope>
    
    </dependency>
    
    HelloFriend
    
    <dependency>
    
    <groupId>junit</groupId>
    
    <artifactId>junit</artifactId>
    
    <version>4.9</version>
    
    <scope>test</scope>
    
    </dependency>
    
    MakeFriend
    
    <dependency>
    
    <groupId>junit</groupId>
    
    <artifactId>junit</artifactId>
    
    <version>4.9</version>
    
    <scope>test</scope>
    
    </dependency>

    此时如果项目需要将各个模块的junit版本统一为4.9,那么到各个工程中手动修改无疑是非常不可取的。使用继承机制就可以将这样的依赖信息统一提取到父工程模块中进行统一管理。

    15.2 创建父工程

    创建父工程和创建一般的Java工程操作一致,唯一需要注意的是:打包方式处要设置为pom。

    15.3 在子工程中引用父工程

    <parent>
    
    <!-- 父工程坐标 -->
    
    <groupId>...</groupId>
    
    <artifactId>...</artifactId>
    
    <version>...</version>
    
    <relativePath>从当前目录到父项目的pom.xml文件的相对路径</relativePath>
    
    </parent> 
    <parent>
    
    <groupId>com.atguigu.maven</groupId>
    
    <artifactId>Parent</artifactId>
    
    <version>0.0.1-SNAPSHOT</version>
    
    <!-- 指定从当前子工程的pom.xml文件出发,查找父工程的pom.xml的路径 -->
    
    <relativePath>../Parent/pom.xml</relativePath>
    
    </parent>

    此时如果子工程的groupId和version如果和父工程重复则可以删除。

    15.4 在父工程中管理依赖

    将Parent项目中的dependencies标签,用dependencyManagement标签括起来

    <dependencyManagement>
    
    <dependencies>
    
    <dependency>
    
    <groupId>junit</groupId>
    
    <artifactId>junit</artifactId>
    
    <version>4.9</version>
    
    <scope>test</scope>
    
    </dependency>
    
    </dependencies>
    
    </dependencyManagement>

    在子项目中重新指定需要的依赖,删除范围和版本号

    <dependencies>
    
    <dependency>
    
    <groupId>junit</groupId>
    
    <artifactId>junit</artifactId>
    
    </dependency>
    
    </dependencies>

     

  • 相关阅读:
    android面试之怎么把图片变成圆形
    android面试之contentProvider获取联系人
    Android面试之assets和aw文件的使用
    Android设计模式之面试
    Activity、Window、View的关系
    ViewPager的简单用法
    补间动画
    帧动画
    android系统的样式和主题
    C++的三种继承方式简述
  • 原文地址:https://www.cnblogs.com/minmin123/p/11242510.html
Copyright © 2020-2023  润新知