modules:
module模塊拉庶,用來(lái)管理同一個(gè)項(xiàng)目中的各個(gè)模塊睬隶。
如果一個(gè)項(xiàng)目分成好幾個(gè)模塊,那么我們構(gòu)建的時(shí)候就需要每個(gè)模塊各構(gòu)建一次浪南,這樣會(huì)很麻煩晚缩。簡(jiǎn)單的做法是使用聚合尾膊,一次構(gòu)建全部的模塊。
聚合是僅僅負(fù)責(zé)其他的模塊荞彼。
例如:
<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.youzhibing.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Account Aggrregator</name>
<url>http://maven.apache.org</url>
<modules>
<!-- 模塊都寫(xiě)在此處 -->
<module>account-register</module>
<module>account-persist</module>
</modules>
</project>
parent:繼承冈敛,類(lèi)似于Java中的繼承,例如每個(gè)子模塊都用到了spring鸣皂,我們可以不對(duì)每個(gè)子模塊單獨(dú)配置spring的依賴抓谴,
可以在父pom中配置各個(gè)子模塊的公共依賴暮蹂。
<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.youzhibing.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Account Aggrregator</name>
<url>http://maven.apache.org</url>
<modules>
<!-- 模塊都寫(xiě)在此處 -->
<module>account-register</module>
<module>account-persist</module>
</modules>
<dependencies> <!-- 配置共有依賴 -->
<!-- spring 依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- junit 依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在子模塊的pom中配置
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.youzhibing.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- 與不配置一樣,默認(rèn)就是尋找上級(jí)目錄下得pom.xml -->
</parent>
<artifactId>account-register</artifactId>
<name>account-register</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies> <!-- 配置自己獨(dú)有依賴 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.youzhibing.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>account-persist</artifactId>
<name>account-persist</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies> <!-- 配置自己獨(dú)有依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.16</version>
</dependency>
</dependencies>
</project>
依賴管理:
dependencyManagement只是聲明依賴癌压,并不實(shí)現(xiàn)引入仰泻,因此子項(xiàng)目需要顯示的聲明需要的依賴,不然不會(huì)從父項(xiàng)目中繼承滩届。而dependencies即使子項(xiàng)目中不寫(xiě)需要的依賴項(xiàng)集侯,仍然可以從父項(xiàng)目中繼承該依賴項(xiàng)。
繼承可以消除重復(fù)添加丐吓,但是假設(shè)添加一個(gè)新的子模塊浅悉,該模塊只是提供了一些簡(jiǎn)單的幫助工具趟据,不需要依賴spring券犁、junit等,
那么就可以在父pom中使用dependencyManagement元素汹碱,這樣既可以讓子模塊繼承到父模塊的依賴配置粘衬,
又能保證子模塊可以靈活的引入其需要的其他依賴。
在父pom中使用dependencyManagement
<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.youzhibing.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Account Aggrregator</name>
<url>http://maven.apache.org</url>
<modules>
<!-- 模塊都寫(xiě)在此處 -->
<module>account-register</module>
<module>account-persist</module>
</modules>
<dependencyManagement>
<dependencies> <!-- 配置共有依賴 -->
<!-- spring 依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- junit 依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
在account-persist的pom.xml(account-register也一樣) :
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.youzhibing.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>account-persist</artifactId>
<name>account-persist</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- spring 依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- junit 依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.16</version>
</dependency>
</dependencies>
</project>
我們可以看到子pom中添加的依賴沒(méi)有指定版本號(hào)咳促,這樣就可以繼承父pom中聲明的依賴及版本稚新。
import
import只在dependencyManagement元素下才有效果,
作用是將目標(biāo)pom中的dependencyManagement配置導(dǎo)入并合并到當(dāng)前pom的dependencyManagement元素中跪腹,
例如下面就是將account-aggregator中的dependencyManagement配置導(dǎo)入并合并到當(dāng)前POM中褂删。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.youzhibing.account</groupId>
<artifactId>account-aggregator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
其他地方
properties:通過(guò)<properties>元素,用戶可以定義一個(gè)或多個(gè)maven屬性冲茸,這樣就可以在pom的其他地方使用${屬性名}的方式引用該屬性屯阀。
Maven總共有6類(lèi)屬性,內(nèi)置屬性轴术、POM屬性难衰、自定義屬性、Settings屬性逗栽、Java屬性盖袭。
內(nèi)置屬性:
兩個(gè)常用內(nèi)置屬性 ${basedir} 表示項(xiàng)目跟目錄,即包含pom.xml文件的目錄彼宠;${version} 表示項(xiàng)目版本
pom屬性:
用戶可以使用該類(lèi)屬性引用POM文件中對(duì)應(yīng)元素的值鳄虱。如${project.artifactId}就對(duì)應(yīng)了<project> <artifactId>元素的值,常用的POM屬性包括:
${project.build.sourceDirectory}:項(xiàng)目的主源碼目錄凭峡,默認(rèn)為src/main/java/
${project.build.testSourceDirectory}:項(xiàng)目的測(cè)試源碼目錄醇蝴,默認(rèn)為src/test/java/
${project.build.directory} : 項(xiàng)目構(gòu)建輸出目錄,默認(rèn)為target/
${project.outputDirectory} : 項(xiàng)目主代碼編譯輸出目錄想罕,默認(rèn)為target/classes/
${project.testOutputDirectory}:項(xiàng)目測(cè)試主代碼輸出目錄悠栓,默認(rèn)為target/testclasses/
${project.groupId}:項(xiàng)目的groupId
${project.artifactId}:項(xiàng)目的artifactId
${project.version}:項(xiàng)目的version,與${version} 等價(jià)
${project.build.finalName}:項(xiàng)目打包輸出文件的名稱霉涨,默認(rèn)為${project.artifactId}-${project.version}
等。