[TOC]
# 繼承
多次使用到的依賴,比如:單元測試,沒有必要在所有的項目中都引用一下,此時就可以采用繼承的方式來實現,先來一個父級的POM.XML,然后再繼承此POM.XML。
1. packaging 改為pom。
2. dependencyManagement 中并不提取依賴。
3. 父類parent中的main和test沒有意義,可以刪除。
父類pom:
```
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>3.8.1</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
```
> 可以在pom.xml文件中定義Properties屬性,自定義標簽。
然后在相應位置引用的形式是${標簽名}即可。
子類pom:
```
<parent>
<groupId>com.xxx</groupId>
<artifactId>xxx-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
```
# 聚合
對于多個項目一起編譯,這種方式叫做聚合。
還以前面的ABC三個項目作為例子說明;
三者設定了傳遞依賴關系,并且經過配置,但是此時不想一個一個的由高到低的編譯/打包/安裝,此時可以在A的pom.xml文件的根目錄中加入如下元素標簽,只需要對A進行打包安裝即可實現對B和C的打包和安裝,這種一同編譯安裝的方式,就叫做聚合。
```
<modules>
<module>../C</module>
<module>../B</module>
<module>../A</module>
</modules>
```