• 【Maven学习】3.9 实验九:继承


    1、概念

    Maven工程之间,A 工程继承 B 工程

    • B 工程:父工程
    • A 工程:子工程

    本质上是 A 工程的 pom.xml 中的配置继承了 B 工程中 pom.xml 的配置。

    2、作用

    在父工程中统一管理项目中的依赖信息,具体来说是管理依赖信息的版本。

    它的背景是:

    • 对一个比较大型的项目进行了模块拆分。
    • 一个 project 下面,创建了很多个 module。
    • 每一个 module 都需要配置自己的依赖信息。

    它背后的需求是:

    • 在每一个 module 中各自维护各自的依赖信息很容易发生出入,不易统一管理。
    • 使用同一个框架内的不同 jar 包,它们应该是同一个版本,所以整个项目中使用的框架版本需要统一。
    • 使用框架时所需要的 jar 包组合(或者说依赖信息组合)需要经过长期摸索和反复调试,最终确定一个可用组合。这个耗费很大精力总结出来的方案不应该在新的项目中重新摸索。

    通过在父工程中为整个项目维护依赖信息的组合既保证了整个项目使用规范、准确的 jar 包;又能够将以往的经验沉淀下来,节约时间和精力。

    3、举例

    在一个工程中依赖多个 Spring 的 jar 包

    TIP

    [INFO] ± org.springframework:spring-core:jar:4.0.0.RELEASE:compile
    [INFO] | - commons-logging:commons-logging:jar:1.1.1:compile
    [INFO] ± org.springframework:spring-beans:jar:4.0.0.RELEASE:compile
    [INFO] ± org.springframework:spring-context:jar:4.0.0.RELEASE:compile
    [INFO] ± org.springframework:spring-expression:jar:4.0.0.RELEASE:compile
    [INFO] ± org.springframework:spring-aop:jar:4.0.0.RELEASE:compile
    [INFO] | - aopalliance:aopalliance:jar:1.0:compile

    使用 Spring 时要求所有 Spring 自己的 jar 包版本必须一致。为了能够对这些 jar 包的版本进行统一管理,我们使用继承这个机制,将所有版本信息统一在父工程中进行管理。

    4、操作

    ①创建父工程

    创建的过程和前面创建 pro01-maven-java 一样。

    工程名称:pro03-maven-parent

    工程创建好之后,要修改它的打包方式:

      <groupId>com.atguigu.mavengroupId>
      <artifactId>pro03-maven-parentartifactId>
      <version>1.0-SNAPSHOTversion>
    
      
      <packaging>pompackaging>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    只有打包方式为 pom 的 Maven 工程能够管理其他 Maven 工程。打包方式为 pom 的 Maven 工程中不写业务代码,它是专门管理其他 Maven 工程的工程。

    ②创建模块工程

    模块工程类似于 IDEA 中的 module,所以需要进入 pro03-maven-parent 工程的根目录,然后运行 mvn archetype:generate 命令来创建模块工程。

    假设,我们创建三个模块工程:

    img

    ③查看被添加新内容的父工程 pom.xml

    下面 modules 和 module 标签是聚合功能的配置

    <modules>  
    	<module>pro04-maven-modulemodule>
    	<module>pro05-maven-modulemodule>
    	<module>pro06-maven-modulemodule>
    modules>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ④解读子工程的pom.xml

    
    <parent>
    	
    	<groupId>com.atguigu.mavengroupId>
    	<artifactId>pro03-maven-parentartifactId>
    	<version>1.0-SNAPSHOTversion>
    parent>
    
    
    
    
    <artifactId>pro04-maven-moduleartifactId>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ⑤在父工程中配置依赖的统一管理

    
    
    <dependencyManagement>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframeworkgroupId>
    			<artifactId>spring-coreartifactId>
    			<version>4.0.0.RELEASEversion>
    		dependency>
    		<dependency>
    			<groupId>org.springframeworkgroupId>
    			<artifactId>spring-beansartifactId>
    			<version>4.0.0.RELEASEversion>
    		dependency>
    		<dependency>
    			<groupId>org.springframeworkgroupId>
    			<artifactId>spring-contextartifactId>
    			<version>4.0.0.RELEASEversion>
    		dependency>
    		<dependency>
    			<groupId>org.springframeworkgroupId>
    			<artifactId>spring-expressionartifactId>
    			<version>4.0.0.RELEASEversion>
    		dependency>
    		<dependency>
    			<groupId>org.springframeworkgroupId>
    			<artifactId>spring-aopartifactId>
    			<version>4.0.0.RELEASEversion>
    		dependency>
    	dependencies>
    dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    ⑥子工程中引用那些被父工程管理的依赖

    关键点:省略版本号

    
    
    
    
    <dependencies>
    	<dependency>
    		<groupId>org.springframeworkgroupId>
    		<artifactId>spring-coreartifactId>
    	dependency>
    	<dependency>
    		<groupId>org.springframeworkgroupId>
    		<artifactId>spring-beansartifactId>
    	dependency>
    	<dependency>
    		<groupId>org.springframeworkgroupId>
    		<artifactId>spring-contextartifactId>
    	dependency>
    	<dependency>
    		<groupId>org.springframeworkgroupId>
    		<artifactId>spring-expressionartifactId>
    	dependency>
    	<dependency>
    		<groupId>org.springframeworkgroupId>
    		<artifactId>spring-aopartifactId>
    	dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    ⑦在父工程中升级依赖信息的版本

    ……
    			<dependency>
    				<groupId>org.springframeworkgroupId>
    				<artifactId>spring-beansartifactId>
    				<version>4.1.4.RELEASEversion>
    			dependency>
    ……
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    然后在子工程中运行mvn dependency:list,效果如下:

    TIP

    [INFO] org.springframework:spring-aop:jar:4.1.4.RELEASE:compile
    [INFO] org.springframework:spring-core:jar:4.1.4.RELEASE:compile
    [INFO] org.springframework:spring-context:jar:4.1.4.RELEASE:compile
    [INFO] org.springframework:spring-beans:jar:4.1.4.RELEASE:compile
    [INFO] org.springframework:spring-expression:jar:4.1.4.RELEASE:compile
    img

    ⑧在父工程中声明自定义属性

    
    <properties>
    	<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    	
    	
    	<atguigu.spring.version>4.3.6.RELEASEatguigu.spring.version>
    properties>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在需要的地方使用${}的形式来引用自定义的属性名:

    			<dependency>
    				<groupId>org.springframeworkgroupId>
    				<artifactId>spring-coreartifactId>
    				<version>${atguigu.spring.version}version>
    			dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    真正实现“一处修改,处处生效”。

    5、实际意义

    img

    编写一套符合要求、开发各种功能都能正常工作的依赖组合并不容易。如果公司里已经有人总结了成熟的组合方案,那么再开发新项目时,如果不使用原有的积累,而是重新摸索,会浪费大量的时间。为了提高效率,我们可以使用工程继承的机制,让成熟的依赖组合方案能够保留下来。

    如上图所示,公司级的父工程中管理的就是成熟的依赖组合方案,各个新项目、子系统各取所需即可。

  • 相关阅读:
    老杨说运维|今年这个会议非比寻常
    【云原生 · Kubernetes】部署高可用kube-scheduler集群
    cad文字转arcgis注记
    【产品经理修炼之道】- 从0到1搭建风筝比赛小程序
    Linux云服务器如何快速安装宝塔面板?
    【关于我接触了Uview的Upload】——单图上传,多图上传,遇到的问题总结、直传阿里云Oss
    批量剪辑技巧:视频去色处理,让色彩焕然一新!
    【活动系列】那些年写的比较愚蠢的代码
    小米将推出中端手机,高通骁龙7系列再添一员,能否吸引消费者?
    远程连接服务器的工具?
  • 原文地址:https://blog.csdn.net/weixin_44589991/article/details/126127657