首页 > 其他分享 >3. 聚合和继承

3. 聚合和继承

时间:2023-07-04 12:55:05浏览次数:30  
标签:聚合 工程 项目 继承 jar maven pom

1. 聚合

image

  • 分模块开发后,需要将这四个项目都安装到本地仓库,目前我们只能通过项目 Maven 面板的install​ 来安装,并且需要安装四个,如果我们的项目足够多,那么一个个安装起来还是比较麻烦的
  • 如果四个项目都已经安装成功,当 ssm_pojo 发生变化后,我们就得将 ssm_pojo 重新安装到 maven 仓库,但是为了确保我们对 ssm_pojo 的修改不会影响到其他项目模块,我们需要对所有的模块进行重新编译,那又需要将所有的模块再来一遍

项目少的话还好,但是如果项目多的话,一个个操作项目就容易出现漏掉或重复操作的问题,所以我们就想能不能抽取一个项目,把所有的项目管理起来,以后我们要想操作这些项目,只需要操作这一个项目,其他所有的项目都走一样的流程,这个不就很省事省力。

这就用到了我们接下来要讲解的聚合

  • 所谓聚合:将多个模块组织成一个整体,同时进行项目构建的过程称为聚合

  • 聚合工程:通常是一个不具有业务功能的"空"工程(有且仅有一个 pom 文件)

  • 作用:使用聚合工程可以将多个工程编组,通过对聚合工程进行构建,实现对所包含的模块进行同步构建

    • 当工程中某个模块发生更新(变更)时,必须保障工程中与已更新模块关联的模块同步更新,此时可以使用聚合工程来解决批量模块同步构建的问题。

关于聚合具体的实现步骤为:

步骤 1:创建一个空的 maven 项目

image

步骤 2:将项目的打包方式改为 pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <packaging>pom</packaging>

</project>


<p data-line="469" class="sync-line" style="margin:0;"></p>

说明:项目的打包方式,我们接触到的有三种,分别是

  • jar:默认情况,说明该项目为 java 项目
  • war:说明该项目为 web 项目
  • pom:说明该项目为聚合或继承(后面会讲)项目

步骤 3:pom.xml 添加所要管理的项目

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <packaging>pom</packaging>

    <!--设置管理的模块名称-->
    <modules>
        <module>../maven_02_ssm</module>
        <module>../maven_03_pojo</module>
        <module>../maven_04_dao</module>
    </modules>
</project>


<p data-line="498" class="sync-line" style="margin:0;"></p>

步骤 4:使用聚合统一管理项目

image

测试发现,当maven_01_parent​ 的compile​ 被点击后,所有被其管理的项目都会被执行编译操作。这就是聚合工程的作用。

说明:聚合工程管理的项目在进行运行的时候,会按照项目与项目之间的依赖关系来自动决定执行的顺序和配置的顺序无关。

聚合的知识我们就讲解完了,最后总结一句话就是,聚合工程主要是用来管理项目

2. 继承

我们已经完成了使用聚合工程去管理项目,聚合工程进行某一个构建操作,其他被其管理的项目也会执行相同的构建操作。那么接下来,我们再来分析下,多模块开发存在的另外一个问题,重复配置​ 的问题,我们先来看张图:

image

  • spring-webmvc​、spring-jdbc​ 在三个项目模块中都有出现,这样就出现了重复的内容
  • spring-test​ 只在 ssm_crm 和 ssm_goods 中出现,而在 ssm_order 中没有,这里是部分重复的内容
  • 我们使用的 spring 版本目前是5.2.10.RELEASE​,假如后期要想升级 spring 版本,所有跟 Spring 相关 jar 包都得被修改,涉及到的项目越多,维护成本越高

面对上面的这些问题,我们就得用到接下来要学习的继承

  • 所谓继承:描述的是两个工程间的关系,与 java 中的继承相似,子工程可以继承父工程中的配置信息,常见于依赖关系的继承。

  • 作用:

    • 简化配置
    • 减少版本冲突

接下来,我们到程序中去看看继承该如何实现?

步骤 1:创建一个空的 Maven 项目并将其打包方式设置为 pom

步骤 2:在子项目中设置其父工程

分别在maven_02_ssm​,maven_03_pojo​,maven_04_dao​ 的 pom.xml 中添加其父项目为maven_01_parent


<parent>
    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <!--设置父项目pom.xml位置路径-->
    <relativePath>../maven_01_parent/pom.xml</relativePath>
</parent>

步骤 3:优化子项目共有依赖导入问题

  1. 将子项目共同使用的 jar 包都抽取出来,维护在父项目的 pom.xml 中
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <packaging>pom</packaging>

    <!--设置管理的模块名称-->
    <modules>
        <module>../maven_02_ssm</module>
        <module>../maven_03_pojo</module>
        <module>../maven_04_dao</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
</project>


<p data-line="633" class="sync-line" style="margin:0;"></p>

  1. 删除子项目中已经被抽取到父项目的 pom.xml 中的 jar 包,如在maven_02_ssm​ 的 pom.xml 中将已经出现在父项目的 jar 包删除掉
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.itheima</groupId>
  <artifactId>maven_02_ssm</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <!--配置当前工程继承自parent工程-->
  <parent>
    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <relativePath>../maven_01_parent/pom.xml</relativePath>
  </parent>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.itheima</groupId>
      <artifactId>maven_04_dao</artifactId>
      <version>1.0-SNAPSHOT</version>
      <!--排除依赖是隐藏当前资源对应的依赖关系-->
      <exclusions>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>80</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>




<p data-line="706" class="sync-line" style="margin:0;"></p>

删除完后,你会发现父项目中有依赖对应的 jar 包,子项目虽然已经将重复的依赖删除掉了,但是刷新的时候,子项目中所需要的 jar 包依然存在。

当项目的<parent>​ 标签被移除掉,会发现多出来的 jar 包依赖也会随之消失。

  1. maven_04_dao​ 项目的 pom.xml 中的所有依赖删除,然后添加上maven_01_parent​ 的父项目坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>maven_04_dao</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--配置当前工程继承自parent工程-->
    <parent>
        <groupId>com.itheima</groupId>
        <artifactId>maven_01_parent</artifactId>
        <version>1.0-RELEASE</version>
        <relativePath>../maven_01_parent/pom.xml</relativePath>
    </parent>
</project>


<p data-line="733" class="sync-line" style="margin:0;"></p>

刷新并查看 Maven 的面板,会发现 maven_04_dao 同样引入了父项目中的所有依赖。

image

这样我们就可以解决刚才提到的第一个问题,将子项目中的公共 jar 包抽取到父工程中进行统一添加依赖,这样做的可以简化配置,并且当父工程中所依赖的 jar 包版本发生变化,所有子项目中对应的 jar 包版本也会跟着更新。

image

步骤 4:优化子项目依赖版本问题

如果把所有用到的 jar 包都管理在父项目的 pom.xml,看上去更简单些,但是这样就会导致有很多项目引入了过多自己不需要的 jar 包。如上面看到的这张图:

image

如果把所有的依赖都放在了父工程中进行统一维护,就会导致 ssm_order 项目中多引入了spring-test​ 的 jar 包,如果这样的 jar 包过多的话,对于 ssm_order 来说也是一种"负担"。

那针对于这种部分项目有的 jar 包,我们该如何管理优化呢?

  1. 在父工程 mavne_01_parent 的 pom.xml 来定义依赖管理
<p data-line="756" class="sync-line" style="margin:0;"></p>



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


<p data-line="767" class="sync-line" style="margin:0;"></p>

  1. 将 maven_02_ssm 的 pom.xml 中的 junit 依赖删除掉,刷新 Maven

image

刷新完会发现,在 maven_02_ssm 项目中的 junit 依赖并没有出现,所以我们得到一个结论:

<dependencyManagement>标签不真正引入 jar 包,而是配置可供子项目选择的 jar 包依赖

子项目要想使用它所提供的这些 jar 包,需要自己添加依赖,并且不需要指定<version>

  1. 在 maven_02_ssm 的 pom.xml 添加 junit 的依赖
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>


<p data-line="787" class="sync-line" style="margin:0;"></p>

注意:这里就不需要添加版本了,这样做的好处就是当父工程 dependencyManagement 标签中的版本发生变化后,子项目中的依赖版本也会跟着发生变化

  1. 在 maven_04_dao 的 pom.xml 添加 junit 的依赖
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>


<p data-line="799" class="sync-line" style="margin:0;"></p>

这个时候,maven_02_ssm 和 maven_04_dao 这两个项目中的 junit 版本就会跟随着父项目中的标签 dependencyManagement 中 junit 的版本发生变化而变化。不需要 junit 的项目就不需要添加对应的依赖即可。

至此继承就已经学习完了,总结来说,继承可以帮助做两件事

  • 将所有项目公共的 jar 包依赖提取到父工程的 pom.xml 中,子项目就可以不用重复编写,简化开发

  • 将所有项目的 jar 包配置到父工程的 dependencyManagement 标签下,实现版本管理,方便维护

    • dependencyManagement 标签不真正引入 jar 包,只是管理 jar 包的版本
    • 子项目在引入的时候,只需要指定 groupId 和 artifactId,不需要加 version
    • 当 dependencyManagement 标签中 jar 包版本发生变化,所有子项目中有用到该 jar 包的地方对应的版本会自动随之更新

最后总结一句话就是,父工程主要是用来快速配置依赖 jar 包和管理项目中所使用的资源

小结

继承的实现步骤:

  • 创建 Maven 模块,设置打包类型为 pom

    <packaging>pom</packaging>
    
  • 在父工程的 pom 文件中配置依赖关系(子工程将沿用父工程中的依赖关系),一般只抽取子项目中公有的 jar 包

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        ...
    </dependencies>
    
  • 在父工程中配置子工程中可选的依赖关系

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.16</version>
            </dependency>
        </dependencies>
        ...
    </dependencyManagement>
    
  • 在子工程中配置当前工程所继承的父工程

    <!--定义该工程的父工程-->
    <parent>
        <groupId>com.itheima</groupId>
        <artifactId>maven_01_parent</artifactId>
        <version>1.0-RELEASE</version>
        <!--填写父工程的pom文件,可以不写-->
        <relativePath>../maven_01_parent/pom.xml</relativePath>
    </parent>
    
  • 在子工程中配置使用父工程中可选依赖的坐标

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
    </dependencies>
    

    注意事项:

    1.子工程中使用父工程中的可选依赖时,仅需要提供群组 id 和项目 id,无需提供版本,版本由父工程统一提供,避免版本冲突

    2.子工程中还可以定义父工程中没有定义的依赖关系,只不过不能被父工程进行版本统一管理。

3. 聚合与继承的区别

3.1 聚合与继承的区别

两种之间的作用:

  • 聚合用于快速构建项目,对项目进行管理
  • 继承用于快速配置和管理子项目中所使用 jar 包的版本

聚合和继承的相同点:

  • 聚合与继承的 pom.xml 文件打包方式均为 pom,可以将两种关系制作到同一个 pom 文件中
  • 聚合与继承均属于设计型模块,并无实际的模块内容

聚合和继承的不同点:

  • 聚合是在当前模块中配置关系,聚合可以感知到参与聚合的模块有哪些
  • 继承是在子模块中配置关系,父模块无法感知哪些子模块继承了自己

相信到这里,大家已经能区分开什么是聚合和继承,但是有一个稍微麻烦的地方就是聚合和继承的工程构建,需要在聚合项目中手动添加modules​ 标签,需要在所有的子项目中添加parent​ 标签,万一写错了咋办?

3.2 IDEA 构建聚合与继承工程

其实对于聚合和继承工程的创建,IDEA 已经能帮助我们快速构建,具体的实现步骤为:

步骤 1:创建一个 Maven 项目

创建一个空的 Maven 项目,可以将项目中的src​ 目录删除掉,这个项目作为聚合工程和父工程。

image

步骤 2:创建子项目

该项目可以被聚合工程管理,同时会继承父工程。

image

创建成功后,maven_parent 即是聚合工程又是父工程,maven_web 中也有 parent 标签,继承的就是 maven_parent,对于难以配置的内容都自动生成。

标签:聚合,工程,项目,继承,jar,maven,pom
From: https://www.cnblogs.com/NorthPoet/p/17525478.html

相关文章

  • CSS学习笔记2-CSS的继承_层叠_类型和CSS盒子模型
    1CSS属性继承CSS的某些属性具有继承性(Inherited):如果一个属性具备继承性,那么在该元素上设置后,它的后代元素都可以继承这个属性;当然,如果后代元素自己有设置该属性,那么优先使用后代元素自己的属性(不管继承过来的属性权重多高);如何知道一个属性是否具有继承性呢?......
  • 面向对象编程Python:类、对象和继承
    面向对象编程(Object-OrientedProgramming,简称OOP)是一种常用的编程范式,它将数据和操作数据的函数封装在一起,以创建对象。Python是一种支持面向对象编程的高级编程语言,它提供了类(class)、对象(object)和继承(inheritance)等概念,使得代码更加模块化、可维护性更高。本文将介绍Python中的......
  • Maven高级相关知识:模块,打包方式,依赖继承,聚合,私服搭建
    Maven高级相关知识:模块,打包方式,依赖继承,聚合,私服搭建01.Maven是一款构建和管理Java项目的工具1.Maven高级内容包括:1.分模块设计与开发2.继承与聚合3.私服02.分模块设计:我们在进行项目设计阶段,就可以将一个大的项目拆分成若干个模块,每一个模块都是独立的.分模块设计就是......
  • 高德地图聚合注意版本引用
    initMap(){letthat=thisletpoints=[]try{letlevel=that.mapLevel==='null'||null===that.mapLevel?10:this.mapLevelletcenterPoint=that.mapCenter.split(',')tha......
  • 空调负荷需求响应matlab 建立空调负荷的聚合模型,按照第二章考虑调节空调温度对空调响
    空调负荷需求响应matlab编程语言:matlab内容:建立空调负荷的聚合模型,按照第二章考虑调节空调温度对空调响应潜力的影响,程序结果充分说明随着上调温度的增大,响应程度逐渐增大。程序运行稳定这段代码主要是一个温度控制系统的模拟程序,用于模拟空调系统对室内温度的控制。下面我来逐......
  • MongoDB聚合操作之排序、分页
    聚合操作之排序、分页管道命令之$sort$sort用于将输入的文档排序后输出使用示例如下:查询人物,按照年龄升序db.person.aggregate([{$sort:{age:1}}])查询每个国家的人数,并排序db.person.aggregate([{$group:{_id:"$country",counter:{$sum:1}}},{$sort:{count......
  • 面向对象(三大特征、继承下的查找、super、组合)
    面向对象有三大特征:封装、继承和多态继承继承其实和封装差不多,就是新建的类称为是子类或派生类,多个子类继承同一个类,这个类教父类或基类1.为什么要继承类解决什么问题:解决的是对象与对象之间代码冗余问题继承解决什么问题:解决的是类与类之间的代码冗余问题2.怎样继......
  • 华为交换机链路聚合Eth-trunk LACP配置实例
    链路聚合基本概念:链路聚合是把两台设备之间的链路聚集在一块,当做一条逻辑链路使用。链路聚合的作用:1.增加链路带宽:多个链路聚合成一个带宽更大的链路,一般采用基于流的负载均衡模式;2.增加链路可靠性:通过聚合组的多个链路,若一条物理链路出现问题,不会影响整个逻辑链路聚合组链路;3.实现......
  • MongoDB聚合操作之分组、过滤
    聚合操作之分组、过滤MongoDB中聚合(aggregate)主要用于处理多个文档(诸如统计平均值,求和等),并返回计算后的数据结果。对多个文档进行分组对分组的文档执行操作并返回单个结果分析数据变化语法:db.集合名称.aggregate([{管道:{表达式}}])管道命令之$group按照某个字段......
  • 如何明辨组合、聚合、继承
    提问如何明辨组合、聚合、继承回答组合:功能组合;功能作为类的属性,必须依附类存在;继承:血脉继承;在严格的血脉关系,变化少的场景下使用;聚合:一个独立个体,是另一个独立个体的属性,......