何为继承?
继承为了消除重复,我们把很多相同的配置提取出来
例如:grouptId,version等
父工程设置为被继承
<packaging>pom</packaging>
子工程继承父工程
省略父工程中定义的坐标除访问名称中的所有设定,添加继承父工程
<parent>
<groupId>…</groupId>
<artifactId>… </artifactId>
<version>… </version>
<relativePath>../父工程项目名</relativePath>
</parent>
子工程里可以省略群组id和版本号的配置
父工程统一管理子工程依赖版本
<dependencyManagement>
<dependencies>
//添加公共依赖包
</dependencies>
</dependencyManagement>
子工程仅仅添加依赖包,无需添加版本,版本由父工程继承而来
为了进一步便于管理,将所有的版本管理设置在一起,设置为系统属性值
<properties>
<junit.version>4.9</junit.version>
……
</properties>
引用使用${junit.version}格式进行,只能在依赖范围设置
父工程统一管理子工程依赖关系
如果所有子工程都需要依赖某些包,父工程可以通过设置依赖,将依赖关系传递到子工程中
<dependencies>
//添加公共依赖包
</dependencies>
何为聚合?
如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合
<modules>
<module>../子项目名称1</module>
<module>../子项目名称2</module>
<module>../子项目名称3</module>
</modules>
聚合与继承的关系
聚合主要为了快速构建项目
继承主要为了消除重复
下面是具体的配置代码
<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>cn.itcast</groupId>
<artifactId>ZParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<log4j.version>1.2.17</log4j.version>
<junit.version>4.9</junit.version>
<system.version>0.0.1-SNAPSHOT</system.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency> -->
</dependencies>
<!-- 聚合 -->
<modules>
<module>../One</module>
<module>../Two</module>
<module>../WThree</module>
</modules>
</project>