Maven多模組工程搭建
一、工程結構:
其中ds 系統中包含兩個模組,分別為ds-service和ds-admin
二、搭建如上工程結構步驟如下:
1、建立父專案ds,在Eclipse裡面New -> Maven Project
;記得勾選Create
a simple project”
建立工程時注意一下紅圈:
2、父專案建好之後,將目錄下面將其他無關的檔案刪除,只要留下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.suning.search</groupId>
<artifactId>ps</artifactId>
<packaging>pom</packaging> <!--所有帶有子模組的專案的packaging都為pom-->
<name>ds-parent</name>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>ds-service</module>
</modules>
<dependencyManagement><!--admin和service公用的依賴放在這裡-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
3、建立子模組:
選中剛建的父專案,在彈出選單中點選
New -> Maven Module
;在紅圈中輸入子模組名稱。新增完成後會在父專案的pom中自動新增模組名。
(注意:子模組建立過程如果是一般的模組,保持預設GroupId:org.apache.maven.archetypes,Artifact Id:maven-archetype-quickstart,如果是web模組則選中下圖中的紅圈所示選項)
按上面步驟建立的子專案,在pom.xml
中有個parent
節點,所以,他可以繼承父專案的相關資訊。沒錯,父子專案中存在繼承關係。
在子專案的pom.xml
中,子專案的groupId
和version
一般和父專案相同,那麼可以把子專案的這兩個引數刪除,這樣會自動繼承父專案的取值。
同樣,如果其他的一些屬性,所有子專案都是一樣的,那麼可以上移到父專案中設定,子專案中無需重複設定。比如:<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>可以僅在父專案中設定一次。
Manen提供dependencyManagement
和pluginManagement
兩個標籤。使用這兩個標籤,可以在父專案中統一管理依賴和外掛的配置引數,比如版本號啥的。而在子專案中,僅需列出需要使用的依賴和外掛的groupId
和artifactId
就可以了,其他資訊會自動從父專案管理的資訊裡面獲取。
例如:
在父專案中:
<dependencyManagement><!--admin和service公用的依賴放在這裡-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
子專案中:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>