:notebook: 本文已歸檔到:「blog」html
POM 是 Project Object Model 的縮寫,即項目對象模型。java
pom.xml 就是 maven 的配置文件,用以描述項目的各類信息。git
<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>
<!-- 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>
複製代碼
project
是 pom.xml 中描述符的根。modelVersion
指定 pom.xml 符合哪一個版本的描述符。maven 2 和 3 只能爲 4.0.0。通常 jar 包被識別爲: groupId:artifactId:version
的形式。github
<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>org.codehaus.mojo</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<packaging>war</packaging>
</project>
複製代碼
在 maven 中,根據 groupId
、artifactId
、version
組合成 groupId:artifactId:version
來惟一識別一個 jar 包。web
1.0-SNAPSHOT
。各個部分的含義和處理邏輯以下說明:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>2.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
...
</project>
複製代碼
groupId
、artifactId
、version
意義相同。packaging
的類型,若是不使用 type
標籤,maven 默認爲 jar。scope
標籤,maven 默認爲這個範圍。編譯依賴關係在全部 classpath 中均可用。此外,這些依賴關係被傳播到依賴項目。propertie
來指定特定的路徑,如$ {java.home} / lib。因爲假定先前安裝了系統範圍依賴關係,maven 將不會檢查項目的倉庫,而是檢查庫文件是否存在。若是沒有,maven 將會失敗,並建議您手動下載安裝。optional
讓其餘項目知道,當您使用此項目時,您不須要這種依賴性才能正常工做。groupId
和 artifactId
。與可選項不一樣,可能或可能不會安裝和使用,排除主動從依賴關係樹中刪除本身。maven 支持繼承功能。子 POM 可使用 parent
指定父 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
<artifactId>my-project</artifactId>
</project>
複製代碼
relativePath
元素。在搜索本地和遠程存儲庫以前,它不是必需的,但能夠用做 maven 的指示符,以首先搜索給定該項目父級的路徑。dependencyManagement
是表示依賴 jar 包的聲明。即你在項目中的 dependencyManagement
下聲明瞭依賴,maven 不會加載該依賴,dependencyManagement
聲明能夠被子 POM 繼承。apache
dependencyManagement
的一個使用案例是當有父子項目的時候,父項目中能夠利用 dependencyManagement
聲明子項目中須要用到的依賴 jar 包,以後,當某個或者某幾個子項目須要加載該依賴的時候,就能夠在子項目中 dependencies
節點只配置 groupId
和 artifactId
就能夠完成依賴的引用。api
dependencyManagement
主要是爲了統一管理依賴包的版本,確保全部子項目使用的版本一致,相似的還有plugins
和pluginManagement
。tomcat
子模塊列表。dom
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>
<modules>
<module>my-project</module>
<module>another-project</module>
<module>third-project/pom-example.xml</module>
</modules>
</project>
複製代碼
屬性列表。定義的屬性能夠在 pom.xml 文件中任意處使用。使用方式爲 ${propertie}
。
<project>
...
<properties>
<maven.compiler.source>1.7<maven.compiler.source>
<maven.compiler.target>1.7<maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
...
</project>
複製代碼
build 能夠分爲 "project build" 和 "profile build"。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<!-- "Project Build" contains more elements than just the BaseBuild set -->
<build>...</build>
<profiles>
<profile>
<!-- "Profile Build" contains a subset of "Project Build"s elements -->
<build>...</build>
</profile>
</profiles>
</project>
複製代碼
基本構建配置:
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
複製代碼
defaultGoal : 默認執行目標或階段。若是給出了一個目標,它應該被定義爲它在命令行中(如 jar:jar)。若是定義了一個階段(如安裝),也是如此。
directory :構建時的輸出路徑。默認爲:${basedir}/target
。
finalName :這是項目的最終構建名稱(不包括文件擴展名,例如:my-project-1.0.jar)
filter :定義 * .properties
文件,其中包含適用於接受其設置的資源的屬性列表(以下所述)。換句話說,過濾器文件中定義的「name = value」對在代碼中替換$ {name}字符串。
資源的配置。資源文件一般不是代碼,不須要編譯,而是在項目須要捆綁使用的內容。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<build>
...
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>
...
</testResources>
...
</build>
</project>
複製代碼
* .properties
文件沒必要定義爲進行過濾 - 資源還可使用默認狀況下在 POM 中定義的屬性(例如$ {project.version}),並將其傳遞到命令行中「-D」標誌(例如,「-Dname = value」)或由 properties 元素顯式定義。過濾文件覆蓋上面。${basedir}/src/main/resources
。includes
相似,指定目錄中要排除的文件,使用*做爲通配符。注意:若是 include
和 exclude
發生衝突,maven 會以 exclude
做爲有效項。testResources
與 resources
功能相似,區別僅在於:testResources
指定的資源僅用於 test 階段,而且其默認資源目錄爲:${basedir}/src/test/resources
。<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<extensions>false</extensions>
<inherited>true</inherited>
<configuration>
<classifier>test</classifier>
</configuration>
<dependencies>...</dependencies>
<executions>...</executions>
</plugin>
</plugins>
</build>
</project>
複製代碼
groupId, artifactId, version :和基本配置中的 groupId
、artifactId
、version
意義相同。
extensions :值爲 true 或 false。是否加載此插件的擴展名。默認爲 false。
inherited :值爲 true 或 false。這個插件配置是否應該適用於繼承自這個插件的 POM。默認值爲 true。
configuration - 這是針對我的插件的配置,這裏不擴散講解。
dependencies :這裏的 dependencies
是插件自己所須要的依賴。
executions :須要記住的是,插件可能有多個目標。每一個目標可能有一個單獨的配置,甚至可能將插件的目標徹底綁定到不一樣的階段。執行配置插件的目標的執行。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>echodir</id>
<goals>
<goal>run</goal>
</goals>
<phase>verify</phase>
<inherited>false</inherited>
<configuration>
<tasks>
<echo>Build Dir: ${project.build.directory}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
複製代碼
與 dependencyManagement
很類似,在當前 POM 中僅聲明插件,而不是實際引入插件。子 POM 中只配置 groupId
和 artifactId
就能夠完成插件的引用,且子 POM 有權重寫 pluginManagement 定義。
它的目的在於統一全部子 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
...
</build>
</project>
複製代碼
目錄元素集合存在於 build
元素中,它爲整個 POM 設置了各類目錄結構。因爲它們在配置文件構建中不存在,因此這些不能由配置文件更改。
若是上述目錄元素的值設置爲絕對路徑(擴展屬性時),則使用該目錄。不然,它是相對於基礎構建目錄:${basedir}
。
擴展是在此構建中使用的 artifacts 的列表。它們將被包含在運行構建的 classpath 中。它們能夠啓用對構建過程的擴展(例如爲 Wagon 傳輸機制添加一個 ftp 提供程序),並使活動的插件可以對構建生命週期進行更改。簡而言之,擴展是在構建期間激活的 artifacts。擴展不須要實際執行任何操做,也不包含 Mojo。所以,擴展對於指定普通插件接口的多個實現中的一個是很是好的。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
...
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>1.0-alpha-3</version>
</extension>
</extensions>
...
</build>
</project>
複製代碼
報告包含特定針對 site
生成階段的元素。某些 maven 插件能夠生成 reporting
元素下配置的報告,例如:生成 javadoc 報告。reporting
與 build
元素配置插件的能力類似。明顯的區別在於:在執行塊中插件目標的控制不是細粒度的,報表經過配置 reportSet
元素來精細控制。而微妙的區別在於 reporting
元素下的 configuration
元素能夠用做 build
下的 configuration
,儘管相反的狀況並不是如此( build
下的 configuration
不影響 reporting
元素下的 configuration
)。
另外一個區別就是 plugin
下的 outputDirectory
元素。在報告的狀況下,默認輸出目錄爲 ${basedir}/target/site
。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<reporting>
<plugins>
<plugin>
...
<reportSets>
<reportSet>
<id>sunlink</id>
<reports>
<report>javadoc</report>
</reports>
<inherited>true</inherited>
<configuration>
<links>
<link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
</links>
</configuration>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
...
</project>
複製代碼
項目信息相關的這部分標籤都不是必要的,也就是說徹底能夠不填寫。
它的做用僅限於描述項目的詳細信息。
下面的示例是項目信息相關標籤的清單:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<!-- 項目信息 begin -->
<!--項目名-->
<name>maven-notes</name>
<!--項目描述-->
<description>maven 學習筆記</description>
<!--項目url-->
<url>https://github.com/dunwu/maven-notes</url>
<!--項目開發年份-->
<inceptionYear>2017</inceptionYear>
<!--開源協議-->
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<!--組織信息(如公司、開源組織等)-->
<organization>
<name>...</name>
<url>...</url>
</organization>
<!--開發者列表-->
<developers>
<developer>
<id>victor</id>
<name>Zhang Peng</name>
<email>forbreak at 163.com</email>
<url>https://github.com/dunwu</url>
<organization>...</organization>
<organizationUrl>...</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>+8</timezone>
<properties>...</properties>
</developer>
</developers>
<!--代碼貢獻者列表-->
<contributors>
<contributor>
<!--標籤內容和<developer>相同-->
</contributor>
</contributors>
<!-- 項目信息 end -->
...
</project>
複製代碼
這部分標籤都很是簡單,基本都能作到顧名思義,且都屬於無關緊要的標籤,因此這裏僅簡單介紹一下:
name - 項目完整名稱
description - 項目描述
url - 通常爲項目倉庫的 host
inceptionYear - 開發年份
licenses - 開源協議
organization - 項目所屬組織信息
developers - 項目開發者列表
contributors - 項目貢獻者列表,<contributor>
的子標籤和 <developer>
的徹底相同。
這定義了所使用的缺陷跟蹤系統(Bugzilla,TestTrack,ClearQuest 等)。雖然沒有什麼能夠阻止插件使用這些信息的東西,但它主要用於生成項目文檔。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<issueManagement>
<system>Bugzilla</system>
<url>http://127.0.0.1/bugzilla/</url>
</issueManagement>
...
</project>
複製代碼
CI 構建系統配置,主要是指定通知機制以及被通知的郵箱。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<ciManagement>
<system>continuum</system>
<url>http://127.0.0.1:8080/continuum</url>
<notifiers>
<notifier>
<type>mail</type>
<sendOnError>true</sendOnError>
<sendOnFailure>true</sendOnFailure>
<sendOnSuccess>false</sendOnSuccess>
<sendOnWarning>false</sendOnWarning>
<configuration><address>continuum@127.0.0.1</address></configuration>
</notifier>
</notifiers>
</ciManagement>
...
</project>
複製代碼
郵件列表
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<mailingLists>
<mailingList>
<name>User List</name>
<subscribe>user-subscribe@127.0.0.1</subscribe>
<unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe>
<post>user@127.0.0.1</post>
<archive>http://127.0.0.1/user/</archive>
<otherArchives>
<otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>
</otherArchives>
</mailingList>
</mailingLists>
...
</project>
複製代碼
SCM(軟件配置管理,也稱爲源代碼/控制管理或簡潔的版本控制)。常見的 scm 有 svn 和 git 。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<scm>
<connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
<developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
<tag>HEAD</tag>
<url>http://127.0.0.1/websvn/my-project</url>
</scm>
...
</project>
複製代碼
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<prerequisites>
<maven>2.0.6</maven>
</prerequisites>
...
</project>
複製代碼
repositories
是遵循 Maven 存儲庫目錄佈局的 artifacts 集合。默認的 Maven 中央存儲庫位於https://repo.maven.apache.org/maven2/上。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<repositories>
<repository>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
...
</pluginRepositories>
...
</project>
複製代碼
與 repositories
差很少。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<distributionManagement>
...
<downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>
<status>deployed</status>
</distributionManagement>
...
</project>
複製代碼
它管理在整個構建過程當中生成的 artifact 和支持文件的分佈。從最後的元素開始:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<distributionManagement>
...
<downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>
<status>deployed</status>
</distributionManagement>
...
</project>
複製代碼
repository - 與 repositories
類似
site - 站點信息
relocation - 項目遷移位置
activation
是一個 profile
的關鍵。配置文件的功能來自於在某些狀況下僅修改基本 POM 的功能。這些狀況經過 activation
元素指定。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>sparrow-type</name>
<value>African</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
...
</profile>
</profiles>
</project>
複製代碼