1. 程式人生 > >Eclipse創建Maven多模塊工程

Eclipse創建Maven多模塊工程

other 相關信息 logs png class 選擇 方式 test pri

一、創建父項目

【New】->【Maven Project】

在彈出界面中選擇【Create a simple project...】

技術分享

技術分享

二、創建子項目

選中剛建的父項目,在彈出菜單中點擊【New】->【Other】->【Maven Module】

技術分享

技術分享

技術分享

技術分享

三、優化配置

按上面步驟創建的子項目,在pom.xml中有個parent節點,所以,他可以繼承父項目的相關信息。

在子項目的pom.xml中,子項目的groupIdversion一般和父項目相同,那麽可以把子項目的這兩個參數刪除,這樣會自動繼承父項目的取值。

同樣,如果其他的一些屬性,所有子項目都是一樣的,那麽可以上移到父項目中設置,子項目中無需重復設置。比如:<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

可以僅在父項目中設置一次。

除了這種情況以外,還有一種情況,就是依賴和插件。依賴和插件的情況是這樣,某一個依賴或插件可能會被大部分子項目所使用,但是也可能有些子項目不需要使用,這樣使用上述的方式,簡簡單單地進行繼承就不合適。

Manen提供dependencyManagementpluginManagement兩個標簽。使用這兩個標簽,可以在父項目中統一管理依賴和插件的配置參數,比如版本號。而在子項目中,僅需列出需要使用的依賴和插件的groupIdartifactId,其他信息會自動從父項目管理的信息裏面獲取。

例子,父項目:

<dependencyManagement>
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <
groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> </dependencies> </dependencyManagement>

子項目:

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
  </dependency>
</dependencies>

參考:

http://ju.outofmemory.cn/entry/75620

http://www.cnblogs.com/qiyebao/p/5300539.html

Eclipse創建Maven多模塊工程