4 避坑指南
- 對于多子產品的項目,注意父 pom 會設定 JDK 版本,注意對齊版本号!

項目對象模型 POM
它是使用Maven工作時的基本元件,是一個xml檔案。它被放在工程根目錄下,檔案命名為pom.xml。
POM包含了關于工程和各種配置細節的資訊,Maven使用這些資訊建構工程。
POM 即 Project Object Module,項目對象模型,在 pom.xml 檔案中定義了項目的基本資訊、源代碼、配置檔案、開發者的資訊和角色、問題追蹤系統、組織資訊、項目授權、項目的 url、以及建構項目所用的插件,依賴繼承關系。開發人員需按照 maven 定義的規則進行 POM 檔案的編寫
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<! – The Basics – >
<groupId> … </groupId>
<artifactId> … </artifactId>
<version> … </version>
<packaging> … </packaging>
<dependencies> … </dependencies>
<parent> … </parent>
<dependencyManagement> … </dependencyManagement>
<modules> … </modules>
<properties> … </properties>
<! – Build Settings – >
<build> … </build>
<reporting> … </reporting>
<! – More Project Information – >
<name> … </name>
<description> … </description>
<url> … </url>
<inceptionYear> … </inceptionYear>
<licenses> … </licenses>
<organization> … </organization>
<developers> … </developers>
<contributors> … </contributors>
<! – Environment Settings – >
<issueManagement> … </issueManagement>
<ciManagement> … </ciManagement>
<mailingLists> … </mailingLists>
<scm> … </scm>
<prerequisites> … </prerequisites>
<repositories> … </repositories>
<pluginRepositories> … </pluginRepositories>
<distributionManagement> … </distributionManagement>
<profiles> … </profiles>
</project>
在每個 POM 檔案中都含有的元素是該 project 的坐标,包含三個基本元素:
groupId 定義了項目屬于哪個組,這有助于在大的範圍上差別項目
artifactId 定義了這個項目在組中唯一的 ID,通常是工程的名稱
groupId 和 artifactId 一起定義了artifact 在倉庫中的位置。
name 是一個使用者友好的項目名稱
除了項目坐标外,modelVersion 指定 POM 模型的版本,version 指明目前項目的版本,packaging 指定了項目釋出時的打包類型。
Maven 插件和倉庫
Maven 本質上是一個插件架構,它的核心并不執行任何具體的建構任務,僅僅定義了抽象的生命周期,所有這些任務都交給插件來完成的。每個插件都能完成至少一個任務,每個任務即是一個功能,将這些功能應用在建構過程的不同生命周期中。這樣既能保證拿來即用,又能保證 maven 本身的繁雜和備援。
将生命周期的階段與插件目标互相綁定,就可以在特定的階段完成具體的建構任務。例如下面的代碼就是要在 validate 這個階段執行 maven-antrun-plugin 的 run 目标,具體的任務在 元素中定義。
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>version</id>
<phase>validate</phase>
<configuration>
<target>
具體任務
</target>
</configuration>
<goals>
<goal> run </goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Maven 項目中的插件,依賴和項目建構的輸出都可以由 Maven 的坐标進行唯一的區分,基于這種機制,Maven 将所有項目的構件檔案放置在一個統一的位置,也就是 Maven 倉庫。所有 Maven 項目可以從同一個 Maven 倉庫中擷取自己所需要的依賴 JAR,這節省了磁盤資源。實際的 Maven 項目中不需要存儲依賴的檔案,隻需要在 POM 檔案中生成依賴關系,在建構的時候 Maven 就會自動去倉庫中下載下傳。
在安裝了 Maven 的機器上,會生成一個 ~.m2\repository 目錄,這個目錄被稱為本地倉庫,當 Maven 查找需要的依賴時,首先會在本地查找,如果本地倉庫中存在,則直接使用,否則 Maven 回去遠端倉庫查找,查找到後下載下傳到本地進行使用。遠端中央倉庫的位址為
http://repo1.maven.org/。當然還有一些鏡像倉庫可供使用,有興趣的讀者可以參考 Maven 官方網站的相關介紹。
當個人所在的網絡無法通路公共的 Maven 倉庫時,可以在 settings.xml 中設定代理伺服器。打開 ~.m2\settings.xml,如果沒有則複制 $Maven_HOME/conf/settings.xml 到此路徑下,加入
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host> 代理位址 </host>
<port>8080</port>
<username> 使用者名 </username>
<password> 密碼 </password>
</proxy>
</proxies>