1有時候一個項目中有很多個子產品,要想各個子產品之間影響較小,可以将每個子產品拿出來作為一個項目子產品,對每個項目子產品進行獨立的開發。
2在這些過程中會遇到關于聚合和繼承的問題。
3何為聚合?
a如果我們想一次建構多個項目子產品,那我們就需要對多個項目子產品進行聚合,也就是說當我們點選一個pom的時候,同時運作多個項目子產品。這時候要用到以下的标簽,将各個項目子產品整合在一起。
<modules>
<module>…</module>
</modules>
4何為繼承
a繼承是為了消除重複,我們把很多相同的配置提取出來。(這樣的好處是:統一管理jar包,友善更新)
b例如:groupid,version等。
5聚合與繼承的關系:
a聚合主要是為了快速建構項目
b繼承主要是為了消除重複。
案例(先簡單概括,後全部列出):
一:聚合配置代碼(注意,下面這些代碼在父項目子產品中)
<module>../hello</module>
<module>../hellofriend</module>
<module>../makefriends</module>
注意:其中module的路徑為相對路徑。
二:繼承配置代碼(下面配置在各個子項目子產品中)
<parent>
<groupid>cn.toto.maven</groupid>
<artifactid>project</artifactid>
<version>0.0.1-snapshot</version>
<!—相對父pom的位置,也就是說通過下面的配置找到父pom的位置-->
<relativepath>../parent/pom.xml</relativepath>
</parent>
三、繼承代碼過程中,可以定義屬性
<properties>
<project.build.sourceencoding>utf-8</project.build.sourceencoding>
<junit.version>4.9</junit.version>
<maven.version>0.0.1-snapshot</maven.version>
</properties>
通路屬性的方式為${junit.version},例如:
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
四:在父子產品中用dependencymanagement進行管理(通過下面的配置可以知道每個子產品是什麼,同時:這樣的好處是子子產品可以有選擇行的繼承,而不需要全部繼承)
<dependencymanagement>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<groupid>cn.toto.maven</groupid>
<artifactid>hellofriend</artifactid>
<version>${maven.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependencies>
</dependencymanagement>
6實際案例進行說明
子產品結構:
一:父子產品(parent):
<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.toto.maven</groupid>
<artifactid>parent</artifactid>
<version>0.0.1-snapshot</version>
<packaging>pom</packaging> <!---注意,這裡要寫成pom-->
<name>parent</name>
<!—為實作聚合,點選這個pom.xml的時候四個同時運作--->
<module>../hello</module>
<module>../hellofriend</module>
<module>../makefriends</module>
<module>../web</module>
<!—屬性配置--à
<properties>
</properties>
<!—對所有子產品進行統一的管理-à
<dependencymanagement>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>4.9</version>
<scope>test</scope>
</dependency>
<groupid>cn.itcast.maven</groupid>
<artifactid>hellofriend</artifactid>
<version>0.0.1-snapshot</version>
<scope>compile</scope>
<groupid>cn.itcast.maven</groupid>
<artifactid>hello</artifactid>
<version>0.0.1-snapshot</version>
<scope>compile</scope>
</dependency>
<dependency>
<artifactid>makefriends</artifactid>
</dependencies>
</dependencymanagement>
<distributionmanagement>
<repository>
<id>releases</id>
<name>internal releases</name>
<url>http://localhost:8080/nexus-2.1.2/content/repositories/releases/</url>
</repository>
<snapshotrepository>
<id>snapshots</id>
<name>internal snapshots</name>
<url>http://localhost:8080/nexus-2.1.2/content/repositories/snapshots/</url>
</snapshotrepository>
</distributionmanagement>
</project>
沒有其它的代碼了。
二:hello子產品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">
<!---此處去掉了些公共的配置,但是在parent中的pom.xml中有相關的配置-->
<artifactid>hello</artifactid>
<name>hello</name>
<parent>
<groupid>cn.itcast.maven</groupid>
<artifactid>parent</artifactid>
<version>0.0.1-snapshot</version>
<relativepath>../parent/pom.xml</relativepath>
</parent>
<dependencies>
</dependencies>
相關java代碼略
三:hellofriend(pom.xml)
<artifactid>hellofriend</artifactid>
<name>hellofriend</name>
<parent>
<groupid>cn.toto.maven</groupid>
<groupid>cn.toto.maven</groupid>
<artifactid>hello</artifactid>
makefriends(pom.xml配置)
<artifactid>makefriends</artifactid>
<name>makefriends</name>
<artifactid>hellofriend</artifactid>