關於maven的聚合,我們現在假想一個場景:
如果我有三個maven項目,我需要將這三個項目打包發佈到本地倉庫,按照一般的做法,我會一個一個的右鍵:run as-->Maven build--->install,然後等待控制檯出現build success,那麼如果我有8個、10個甚至更多的maven項目需要打包發佈,是否我要每個都這樣操作呢(估計要累死,而且還不好維護),這個時候就用到了maven聚合。按照這個場景,我們新建三個maven項目,分別爲A,B,C,其pom.xml分別爲:
<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.cn.maven1</groupId>
<artifactId>A</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>A</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
-------------
<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.cn.maven1</groupId>
<artifactId>B</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>B</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
------------
<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.cn.maven1</groupId>
<artifactId>C</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>C</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
-------
這是我們新建一個統一管理他們的maven項目,作爲聚合的載體進行統一打包發佈,pom.xml如下:
<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.cn.maven1</groupId>
<artifactId>ABC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 聚合載體需要打包成pom格式 -->
<packaging>pom</packaging>
<name>ABC</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 添加需要聚合的項目的模塊名,這裏模塊名指的就是項目名 -->
<modules>
<module>../A</module>
<module>../B</module>
<module>../C</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
最後,選中該pom.xml,右鍵run as-->Maven build--->install。控制檯會依次對A,B,C進行打包發佈。
-------------------------------------------------
關於maven的繼承,這個跟java的繼承有點類似,假想在以上A、B、C三個項目中都會引入junit的jar包(實際項目中可能有很多重疊的包),這時我們想要將該包封裝一下,作爲一個統一讓大家來調用,這樣一來方便管理,而來不用每次都去找或者由於版本不同而引發衝突等問題。怎麼來做呢?
首先新建一個maven項目,我們把它叫做父類maven容器,其pom.xml配置如下:
<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>parent</groupId>
<artifactId>ABC_parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ABC_parent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 可將某個標籤定義在properties中作爲變量來被引用,引用方式支持EL表達式 -->
<junit-version>3.8.1</junit-version>
</properties>
<!--dependencyManagement用來管理所有的依賴關係,不會運行 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 利用EL表達式來引用變量 -->
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>