6.Maven聚合和继承,相关案例配置
1有時候一個項目中有很多個模塊,要想各個模塊之間影響較小,可以將每個模塊拿出來作為一個項目模塊,對每個項目模塊進行獨立的開發(fā)。
?
2在這些過程中會遇到關于聚合和繼承的問題。
?
3何為聚合?
A如果我們想一次構建多個項目模塊,那我們就需要對多個項目模塊進行聚合,也就是說當我們點擊一個pom的時候,同時運行多個項目模塊。這時候要用到以下的標簽,將各個項目模塊整合在一起。
<modules>
<module>…</module>
</modules>
?
4何為繼承
A繼承是為了消除重復,我們把很多相同的配置提取出來。(這樣的好處是:統(tǒng)一管理jar包,方便升級)
B例如:groupId,version等。
?
5聚合與繼承的關系:
A聚合主要是為了快速構建項目
B繼承主要是為了消除重復。
案例(先簡單概括,后全部列出):
| 一:聚合配置代碼(注意,下面這些代碼在父項目模塊中) |
| <modules> ??<module>../Hello</module> ??<module>../HelloFriend</module> ??<module>../MakeFriends</module> </modules> |
| 注意:其中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> ??????<dependency> ????????? <groupId>cn.toto.maven</groupId> ????????? <artifactId>HelloFriend</artifactId> ????????? <version>${maven.version}</version> ????????? <type>jar</type> ????????? <scope>compile</scope> ??????</dependency> ????</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> ?<url>http://maven.apache.org</url> ? <!—為實現(xiàn)聚合,點擊這個pom.xml的時候四個同時運行---> <modules> ?????<module>../Hello</module>? ?????<module>../HelloFriend</module>????????? ?????<module>../MakeFriends</module> ?????<module>../web</module> </modules> ? ?<!—屬性配置--à ?<properties> ???<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> ?</properties> ? ?<!—對所有模塊進行統(tǒng)一的管理-à ?<dependencyManagement> ?<dependencies> ???<dependency> ?????<groupId>junit</groupId> ?????<artifactId>junit</artifactId> ?????<version>4.9</version> ?????<scope>test</scope> ???</dependency> ???<dependency> ?????<groupId>cn.itcast.maven</groupId> ???????<artifactId>HelloFriend</artifactId> ?????<version>0.0.1-SNAPSHOT</version> ?????<scope>compile</scope> ???</dependency> ???<dependency> ???????????????????????????<groupId>cn.itcast.maven</groupId> ???????????????????????????<artifactId>Hello</artifactId> ???????????????????????????<version>0.0.1-SNAPSHOT</version> ???????????????????????????<scope>compile</scope> ??????????????????</dependency> ??????????????????<dependency> ???????????????????????????<groupId>cn.itcast.maven</groupId> ???????????????????????????<artifactId>MakeFriends</artifactId> ???????????????????????????<version>0.0.1-SNAPSHOT</version> ???????????????????????????<scope>compile</scope> ??????????????????</dependency> ?</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"> ?<modelVersion>4.0.0</modelVersion> ?<!---此處去掉了些公共的配置,但是在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> ???<dependency> ?????<groupId>junit</groupId> ?????<artifactId>junit</artifactId> ???</dependency> ???</dependencies> </project> |
| 相關java代碼略 |
| ? |
| 三:HelloFriend(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> ? ?<artifactId>HelloFriend</artifactId> ? ?<name>HelloFriend</name> ? ????????<parent>? ?????????<groupId>cn.toto.maven</groupId> ??????????<artifactId>Parent</artifactId> ?????????<version>0.0.1-SNAPSHOT</version> ???????<relativePath>../Parent/pom.xml</relativePath>? ?</parent> ? ?<dependencies> ???<dependency> ?????<groupId>junit</groupId> ?????<artifactId>junit</artifactId> ???</dependency> ???<dependency> ?????<groupId>cn.toto.maven</groupId> ?????<artifactId>Hello</artifactId> ???</dependency> ???</dependencies> </project> |
| ? |
| MakeFriends(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> ? ?<artifactId>MakeFriends</artifactId> ? ? ? ?<name>MakeFriends</name> ?<parent>? ?????????<groupId>cn.itcast.maven</groupId> ??????????<artifactId>Parent</artifactId> ?????????<version>0.0.1-SNAPSHOT</version> ???????<relativePath>../Parent/pom.xml</relativePath>? ?</parent> ?<dependencies> ???<dependency> ?????<groupId>junit</groupId> ?????<artifactId>junit</artifactId> ???</dependency> ???<dependency> ?????<groupId>cn.toto.maven</groupId> ?????<artifactId>HelloFriend</artifactId> ???</dependency> ???</dependencies> </project> |
| 相關java代碼略 |
?
總結
以上是生活随笔為你收集整理的6.Maven聚合和继承,相关案例配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哪里能买到纯蜂蜜 寻找纯天然蜂蜜的好去处
- 下一篇: 水床和圆床的区别?