当前位置: 首页 > news >正文

Maven学习总结(五)——聚合与继承

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Maven学习总结(五)——聚合与继承

一、聚合

  如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合

1.1、聚合配置代码

1 <modules>
2       <module>模块一</module>
3       <module>模块二</module>
4       <module>模块三</module>
5 </modules>

  例如:对项目的Hello、HelloFriend、MakeFriends这三个模块进行聚合

1 <modules>
2       <module>../Hello</module>  
3       <module>../HelloFriend</module>        
4       <module>../MakeFriends</module>
5 </modules>

  其中module的路径为相对路径。

二、继承

  继承为了消除重复,我们把很多相同的配置提取出来,例如:grouptId,version等

2.1、继承配置代码

1 <parent>  
2          <groupId>me.gacl.maven</groupId>
3          <artifactId>ParentProject</artifactId>
4          <version>0.0.1-SNAPSHOT</version>
5          <relativePath>../ParentProject/pom.xml</relativePath>  
6 </parent>

2.2、继承代码中定义属性

  继承代码过程中,可以定义属性,例如:

1 <properties>
2     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3     <junit.version>4.9</junit.version>
4     <maven.version>0.0.1-SNAPSHOT</maven.version>
5 </properties>

  访问属性的方式为${junit.version},例如:

1 <dependency>
2     <groupId>junit</groupId>
3     <artifactId>junit</artifactId>
4     <version>${junit.version}</version>
5     <scope>test</scope>
6 </dependency>

2.3、父模块用dependencyManagement进行管理

复制代码
 1 <dependencyManagement>
 2     <dependencies>
 3     <dependency>
 4         <groupId>junit</groupId>
 5         <artifactId>junit</artifactId>
 6         <version>${junit.version}</version>
 7         <scope>test</scope>
 8     </dependency>    
 9     <dependency>
10             <groupId>cn.itcast.maven</groupId>
11             <artifactId>HelloFriend</artifactId>
12             <version>${maven.version}</version>
13             <type>jar</type>
14             <scope>compile</scope>
15        </dependency>
16      </dependencies>
17 </dependencyManagement>
复制代码

  这样的好处是子模块可以有选择行的继承,而不需要全部继承。

三、聚合与继承的关系

  聚合主要为了快速构建项目,继承主要为了消除重复

四、聚合与继承实战演练

  创建四个Maven项目,如下图所示:

   

  这四个项目放在同一个目录下,方便后面进行聚合和继承

  

  Parent项目是其它三个项目的父项目,主要是用来配置一些公共的配置,其它三个项目再通过继承的方式拥有Parent项目中的配置,首先配置Parent项目的pom.xml,添加对项目的Hello、HelloFriend、MakeFriends这三个模块进行聚合以及jar包依赖,pom.xml的配置信息如下:

  Parent项目的pom.xml配置

复制代码
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4 
 5     <groupId>me.gacl.maven</groupId>
 6     <artifactId>Parent</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <packaging>pom</packaging>
 9 
10     <name>Parent</name>
11     <url>http://maven.apache.org</url>
12     
13     <!-- 对项目的Hello、HelloFriend、MakeFriends这三个模块进行聚合 -->
14     <modules>
15         <module>../Hello</module>
16         <module>../HelloFriend</module>
17         <module>../MakeFriends</module>
18     </modules>
19     
20     <!-- 定义属性 -->
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <junit.version>4.9</junit.version>
24         <maven.version>0.0.1-SNAPSHOT</maven.version>
25     </properties>
26 
27     <!-- 用dependencyManagement进行jar包依赖管理 -->
28     <dependencyManagement>
29         <!-- 配置jar包依赖 -->
30         <dependencies>
31             <dependency>
32                 <groupId>junit</groupId>
33                 <artifactId>junit</artifactId>
34                 <!-- 访问junit.version属性 -->
35                 <version>${junit.version}</version>
36                 <scope>test</scope>
37             </dependency>
38             <dependency>
39                 <groupId>me.gacl.maven</groupId>
40                 <artifactId>Hello</artifactId>
41                 <!-- 访问maven.version属性 -->
42                 <version>${maven.version}</version>
43                 <scope>compile</scope>
44             </dependency>
45             <dependency>
46                 <groupId>me.gacl.maven</groupId>
47                 <artifactId>HelloFriend</artifactId>
48                 <!-- 访问maven.version属性 -->
49                 <version>${maven.version}</version>
50             </dependency>
51         </dependencies>
52     </dependencyManagement>
53 </project>
复制代码

  在Hello项目的pom.xml中继承Parent项目的pom.xml配置

复制代码
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3   
 4   <modelVersion>4.0.0</modelVersion>
 5   <artifactId>Hello</artifactId>
 6   
 7       <!-- 继承Parent项目中的pom.xml配置 -->
 8        <parent>  
 9           <groupId>me.gacl.maven</groupId>
10          <artifactId>Parent</artifactId>
11           <version>0.0.1-SNAPSHOT</version>
12           <!-- 使用相对路径 -->
13         <relativePath>../Parent/pom.xml</relativePath>  
14     </parent>
15     
16     <dependencies>
17         <dependency>
18             <groupId>junit</groupId>
19             <artifactId>junit</artifactId>
20         </dependency>
21     </dependencies>
22 </project>
复制代码

  在HelloFriend项目的pom.xml中继承Parent项目的pom.xml配置

复制代码
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <artifactId>HelloFriend</artifactId>
 5     <name>HelloFriend</name>
 6     
 7     <!-- 继承Parent项目中的pom.xml配置 -->
 8     <parent>
 9         <groupId>me.gacl.maven</groupId>
10         <artifactId>Parent</artifactId>
11         <version>0.0.1-SNAPSHOT</version>
12         <relativePath>../Parent/pom.xml</relativePath>
13     </parent>
14     <dependencies>
15         <dependency>
16             <!-- Parent项目的pom.xml文件配置中已经指明了要使用的Junit的版本号,因此在这里添加junit的依赖时,
17             可以不指明<version></version>和<scope>test</scope>,会直接从Parent项目的pom.xml继承 -->
18             <groupId>junit</groupId>
19             <artifactId>junit</artifactId>
20         </dependency>
21         <!-- HelloFriend项目中使用到了Hello项目中的类,因此需要添加对Hello.jar的依赖 
22         Hello.jar的<version>和<scope>也已经在Parent项目的pom.xml文件配置中已经指明了
23         因此这里也可以省略不写了
24         -->
25         <dependency>
26             <groupId>me.gacl.maven</groupId>
27             <artifactId>Hello</artifactId>
28         </dependency>
29     </dependencies>
30 </project>
复制代码

  在MakeFriends项目的pom.xml中继承Parent项目的pom.xml配置

复制代码
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <artifactId>MakeFriends</artifactId>
 5     <!-- 继承Parent项目中的pom.xml配置 -->
 6     <parent>
 7         <groupId>me.gacl.maven</groupId>
 8         <artifactId>Parent</artifactId>
 9         <version>0.0.1-SNAPSHOT</version>
10         <relativePath>../Parent/pom.xml</relativePath>
11     </parent>
12     <dependencies>
13         <dependency>
14         <!-- Parent项目的pom.xml文件配置中已经指明了要使用的Junit的版本号,因此在这里添加junit的依赖时,
15             可以不指明<version></version>和<scope>test</scope>,会直接从Parent项目的pom.xml继承 -->
16             <groupId>junit</groupId>
17             <artifactId>junit</artifactId>
18         </dependency>
19         <dependency>
20         <!-- MakeFriends项目中使用到了HelloFriend项目中的类,因此需要添加对HelloFriend.jar的依赖 
21         HelloFriend.jar的<version>和<scope>也已经在Parent项目的pom.xml文件配置中已经指明了
22         因此这里也可以省略不写了
23         -->
24             <groupId>me.gacl.maven</groupId>
25             <artifactId>HelloFriend</artifactId>
26         </dependency>
27     </dependencies>
28 </project>
复制代码

  以上的四个项目的pom.xml经过这样的配置之后,就完成了在Parent项目中聚合Hello、HelloFriend、MakeFriends这三个子项目(子模块),而Hello、HelloFriend、MakeFriends这三个子项目(子模块)也继承了Parent项目中的公共配置,这样就可以使用Maven一次性构建所有的项目了,如下图所示:

  

  选中Parent项目的pom.xml文件→【Run As】→【Maven install】,这样Maven就会一次性同时构建Parent、Hello、HelloFriend、MakeFriends这四个项目,如下图所示:

  

转载于:https://my.oschina.net/zhanghaiyang/blog/594722

相关文章:

  • https遇到自签名证书/信任证书
  • Spring学习总结(2)——Spring的常用注解
  • 三剑客 Handler、Looper 和 MessageQueue
  • 使用brew安装MySQL
  • Log4j学习总结(1)——快速入门教程
  • CMDB基于配置文件加载插件的范例
  • RabbitMQ学习总结(2)——安装、配置与监控
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • Bean注入
  • HashTable详解
  • 《Fluid Engine Development》 学习笔记3-光滑粒子流体动力学
  • Express 相关整合
  • Set集合学习
  • redis与lua
  • 并发数和TPS的理解
  • 2017前端实习生面试总结
  • Angular 响应式表单之下拉框
  • Apache的80端口被占用以及访问时报错403
  • Bootstrap JS插件Alert源码分析
  • CSS实用技巧
  • CSS实用技巧干货
  • ES2017异步函数现已正式可用
  • HashMap剖析之内部结构
  • iOS 颜色设置看我就够了
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • Javascript弹出层-初探
  • java多线程
  • Js实现点击查看全文(类似今日头条、知乎日报效果)
  • session共享问题解决方案
  • vue-cli3搭建项目
  • Vue实战(四)登录/注册页的实现
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 入门级的git使用指北
  • 深入浅出Node.js
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 通过几道题目学习二叉搜索树
  • 我感觉这是史上最牛的防sql注入方法类
  • 学习笔记:对象,原型和继承(1)
  • C# - 为值类型重定义相等性
  • #《AI中文版》V3 第 1 章 概述
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (02)vite环境变量配置
  • (1) caustics\
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (java)关于Thread的挂起和恢复
  • (规划)24届春招和25届暑假实习路线准备规划
  • (六)Hibernate的二级缓存
  • (十) 初识 Docker file
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • (原創) 物件導向與老子思想 (OO)
  • ..回顾17,展望18
  • .Mobi域名介绍
  • .NET : 在VS2008中计算代码度量值
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案