介紹
什么是POM?
POM代表“項目對象模型”。它是一個名為pom.XML
的文件中保存的Maven項目的XML表示。
快速概覽
這是一個直接位于POM項目元素下的元素列表。請注意,modelVersion
包含4.0.0。這是目前唯一支持的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>
<!-- 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>
一個簡單的配置示例
<?xml version="1.0" encoding="UTF-8"?>
<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.study</groupId>
<artifactId>kafka-meter</artifactId>
<version>1.0</version>
<description>Kafka plugin for JMeter</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<apache.jmeter.core>5.4.1</apache.jmeter.core>
<org.log4j>2.11.1</org.log4j>
<sf.kafka.api.core.version>1.18.2</sf.kafka.api.core.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>${apache.jmeter.core}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${org.log4j}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${org.log4j}</version>
</dependency>
<dependency>
<groupId>com.sf.kafka</groupId>
<artifactId>sf-kafka-api-core</artifactId>
<version>${sf.kafka.api.core.version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
基礎(chǔ)配置(The Basics)
POM包含關(guān)于項目的所有必要信息,以及構(gòu)建過程中要使用的插件的配置。
<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-project</artifactId>
<version>1.0</version>
</project>
Maven坐標(biāo)(Coordinates)
上面定義的POM是Maven允許的最小配置。groupId:artifactId:version
都是必需字段(不過,如果從父級繼承groupId
和version
,則不需要顯式定義它們-稍后將詳細(xì)介紹繼承)。這三個字段的作用非常像一個地址和時間戳。這標(biāo)志著倉庫中的一個特定位置,就像Maven項目的坐標(biāo)系一樣:
-
groupId: 這在一個組織或項目中通常是獨一無二的。例如,所有的核心Maven工件都應(yīng)該位于
groupId
org.apache.Maven
下。groupId
不一定使用點符號,例如junit項目。請注意,點標(biāo)記的groupId
不必與項目包含的包結(jié)構(gòu)相對應(yīng)。然而,這是一個很好的做法。當(dāng)存儲在倉庫中時,該組的行為與操作系統(tǒng)中的Java包結(jié)構(gòu)非常相似。點被操作系統(tǒng)特定的目錄分隔符(如Unix中的“/”)所取代,后者成為Base倉庫的相對目錄結(jié)構(gòu)。在給定的示例中,org.codehaus.mojo
組位于目錄$M2_REPO/org/codehaus/mojo
中。 -
artifactId:
artifactId
通常是項目的名稱。盡管groupId
很重要,但組內(nèi)的人很少在討論中提到groupId
(他們通常都是同一個ID,例如MojoHaus項目groupId:org.codehaus.mojo
)。artifactId
和groupId
一起創(chuàng)建了一個Key,將這個項目與世界上其他所有項目區(qū)分開來(至少,它應(yīng)該 ??)。artifactId
和groupId
完全定義了工件在倉庫中的存儲區(qū)。在上述項目中,my-project
位于$M2_REPO/org/codehaus/mojo/my-project
。 -
version: 用于以區(qū)分項目和工件版本。
my-project
版本 1.0文件位于目錄結(jié)構(gòu)$M2_REPO.org/codehaus/mojo/my-project/1.0
中。
上面給出的三個元素指向一個項目的特定版本,讓Maven知道我們在與誰打交道,以及在其軟件生命周期中我們需要它們的時間。
打包(Packaging)
現(xiàn)在我們有了groupId:artifactId:version
的地址結(jié)構(gòu),還有一個標(biāo)準(zhǔn)標(biāo)簽可以給我們一個真正完整的東西:那就是項目的packaging
。上述示例中定義的org.codehaus.mojo:my project:1.0
的示例POM將被打包為一個jar
文件。我們可以通過定義不同的packaging
將其變成一個war
文件:
<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">
...
<packaging>war</packaging>
...
</project>
當(dāng)沒有聲明任何packaging
時,默認(rèn)為jar
。當(dāng)前的核心packaing
值為:pom
、jar
、maven-plugin
、ejb
、war
、ear
、rar
。這些定義了在特定包結(jié)構(gòu)的每個相應(yīng)構(gòu)建生命周期階段執(zhí)行的目標(biāo)的默認(rèn)列表:請參閱Plugin Bindings for default lifecycle Reference詳細(xì)信息。
POM關(guān)系(POM Relationships)
依賴(Dependencies)
POM的基石是其依賴性列表。大多數(shù)項目都依賴于其他項目來正確構(gòu)建和運行。即便Maven為你所做的只是管理這個列表,你也受益很多了。Maven在編譯以及執(zhí)行其它需要它們的插件目標(biāo)時下載并鏈接依賴。此外,Maven會自動引入這些依賴項的依賴項(傳遞依賴項),使你的列表可以只關(guān)注項目所需的依賴項。
<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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
...
</project>
-
groupId,artifactId,version
你會經(jīng)??吹竭@些元素。這三位一體用于計算特定項目的Maven坐標(biāo),將其界定為該項目的依賴項。此計算的目的是選擇一個與所有依賴聲明匹配的版本(由于可傳遞依賴,同一工件可能有多個依賴聲明)。這些值應(yīng)為:
-
groupId, artifactId:直接對應(yīng)依賴關(guān)系的坐標(biāo),
-
version:依賴版本需求說明,用于計算依賴的有效版本。
由于依賴關(guān)系是由Maven坐標(biāo)描述的,你可能會想:“這意味著我的項目只能依賴于Maven工件!”答案是,“當(dāng)然,但這是一件好事。”這迫使你只能依賴Maven可以管理的依賴關(guān)系。
不幸的是,有時項目無法從central Maven 倉庫庫下載。例如,一個項目可能依賴于
例如,一個項目可能依賴于一個擁有封閉源代碼許可證的jar,該許可證阻止它位于central倉庫中。有三種方法可以處理這種情況。
-
使用安裝插件在本地安裝依賴項。該方法是最簡單的推薦方法。例如:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
請注意,地址仍然是必需的,只是這次使用命令行,安裝插件將為您創(chuàng)建一個具有給定地址的POM
-
-
創(chuàng)建自己的倉庫并發(fā)布依賴。這是擁有內(nèi)聯(lián)網(wǎng)的公司最喜歡的方法,需要能夠讓每個人都保持同步。Maven有一個名為
deploy:deploy-file
的目標(biāo),它類似于install:install-file
目標(biāo)。 -
將依賴關(guān)系范圍設(shè)置為
system
并定義systemPath
。但是,不建議這樣做
-
classifier
classifier區(qū)分從相同POM構(gòu)建但內(nèi)容不同的工件。它是一些可選的任意字符串,如果有值的話,它會被附加到工件名稱中版本號后面。
以項目為例來說明這個元素的動機(jī)。假設(shè)有個項目,該項目提供了一個以Java 11為目標(biāo)的工件,但同時也提供了仍然支持Java 1.8的工件。第一個工件可以配備有
classifier
jdk11
,第二個工件配備jdk8
,這樣客戶端可以選擇使用哪一個。classifier
的另一個常見用法是將次要工件附加到項目的主要工件上。如果瀏覽Maven center倉庫庫,你會注意到classifier
sources
和javadoc
用于部署項目源代碼和API文檔以及打包的類文件 -
type
對應(yīng)于所選的依賴項類型。默認(rèn)為jar
。雖然它通常表示依賴項文件名上的擴(kuò)展名,但情況并非總是如此:一個類型可以映射到不同的擴(kuò)展名和classifier。類型通常與所使用的packaging相對應(yīng),盡管情況并非總是如此。類型的一些示例值jar
, ejb-client
和test-jar
:請參見默認(rèn)工件處理程序以獲取列表。新類型可以由將extensions
設(shè)置為true
的插件定義,因此這不是一個完整的列表
-
scope
這個元素指的是手頭任務(wù)(編譯和運行時、測試等)的類路徑,以及如何限制依賴項的傳遞性。有五個作用域可用:
- compile - 這是默認(rèn)作用域,如果未指定則使用默認(rèn)值。編譯依賴項在所有類路徑中都可用。此外,這些依賴關(guān)系會傳播到依賴項目。
- provided - 和compile很像,但表示你希望JDK或容器在運行時提供依賴關(guān)系。它僅在編譯和測試類路徑上可用,并且不可傳遞。
- runtime - 此作用域表示該依賴項不是編譯所必須的,而是執(zhí)行依賴項。它在運行時和測試類路徑中,但不在編譯類路徑中。
- test - 此作用域表示此依賴項不是應(yīng)用程序的正常使用所需,僅適用于測試編譯和執(zhí)行階段。它不是傳遞性的。
-
system - 此作用域類似
provided
。只是你必須提供顯式包含它的JAR。工件始終可用,并且不會在倉庫中查找
-
systemPath
僅依賴項scope
為system
時使用。否則,如果設(shè)置了此元素,則將構(gòu)建失敗。此路徑必須是絕對路徑,因此建議使用屬性來指定特定于機(jī)器的路徑(查看下文的 properties
獲取更多 ),例如${java.home}/lib
。由于假設(shè)系統(tǒng)作用域依賴項是事先安裝的,因此Maven不會檢查項目的倉庫,而是檢查以確保文件存在,如果不存在,Maven將構(gòu)建失敗,并建議你手動下載并安裝它。
-
optional
當(dāng)此項目本身是依賴項時,將依賴項標(biāo)記為可選。例如,假設(shè)一個項目
A
依賴于項目B
來編譯一部分可能在運行時不使用的代碼,那么我們可能不需要所有項目都使用項目B
。因此,如果項目X
添加項目A
作為自己的依賴項,那么Maven根本不需要安裝項目B
。象征性地,如果=>
表示必需的依賴項,而-->
表示可選,構(gòu)建A
時有A=>B
,但構(gòu)建X
時則是X=>A-->B
。簡而言之,
optional
讓其他項目知道,當(dāng)你使用此項目時,不需要此依賴項也能正常工作。
依賴版本需求說明(Dependency Version Requirement Specification)
依賴項version
元素定義了用于計算依賴項版本的版本要求。軟需求可以被依賴關(guān)系圖中其他地方相同工件的不同版本所取代。硬需求要求特定的一個或多個版本,并凌駕于軟需求之上。如果沒有滿足該工件所有硬需求的依賴項版本,則構(gòu)建失敗。
版本需求具有以下語法:
-
1.0
: 要求1.0版本(軟需求)。如果依賴關(guān)系樹的早期版本中未出現(xiàn)其他版本,則使用1.0。 -
[1.0]
: 要求1.0版本。使用且僅使用1.0(硬需求) -
(,1.0]
: 要求<=1.0的任意版本(硬需求) -
[1.2,1.3]
: 要求1.2至1.3(含1.2和1.3)之間的任意版本(硬需求)。 -
[1.0,2.0)
: 1.0 <= x < 2.0; 要求1.0到2.0之間(含1.0,不含2.0)的任意版本(硬需求)。 -
[1.5,)
: 要求大于等于1.5的任意版本(硬需求) -
(,1.0],[1.2,)
: 要求小于或等于1.0、大于或等于1.2但不等于1.1的任意版本(硬需求)。多個需求用逗號分隔。 -
(,1.1),(1.1,)
: 要求除1.1以外的任意版本(硬需求);假設(shè)因為1.1存在嚴(yán)重漏洞。Maven選擇每個項目的最高版本,以滿足該項目依賴項的所有硬性要求。如果沒有一個版本能夠滿足所有的硬性要求,那么構(gòu)建就會失敗。
版本順序說明(Version Order Specification):
如果版本字符串為語法正確的Semantic Versioning 1.0.0版本號,那么在幾乎所有情況下,版本比較都遵循該規(guī)范中概述的優(yōu)先級規(guī)則。這些版本是常見的字母數(shù)字ASCII字符串,如2.15.2-alpha。更準(zhǔn)確地說,如果要比較的版本號都與語義版本規(guī)范中BNF語法中的“有效semver”生成相匹配,則情況也是如此。Maven不考慮該規(guī)范所隱含的任何語義。
重要:這僅適用于Semantic Versioning 1.0.0。Maven版本順序算法與Semantic version2.0.0不兼容。特別是,Maven沒有對加號進(jìn)行特殊處理,也沒有考慮構(gòu)建標(biāo)識符。
當(dāng)版本字符串不遵循Semantic Versioning時,需要一組更復(fù)雜的規(guī)則。Maven坐標(biāo)被分割為點之間的標(biāo)記('.
'),hyphe
Maven坐標(biāo)按點('.
')、連字符('-
'),數(shù)字和字符之間的過渡之間的標(biāo)記(token)進(jìn)行拆分。分隔符將被記錄并將對順序產(chǎn)生影響。數(shù)字和字符之間的過渡相當(dāng)于連字符??盏臉?biāo)記將替換為“0
”。這給出了一系列帶有“.
”或“-
”前綴的版本號(數(shù)字標(biāo)記)和版本限定符(非數(shù)字標(biāo)記)(官方原文:The Maven coordinate is split in tokens between dots ('.
'), hyphens ('-
') and transitions between digits and characters. The separator is recorded and will have effect on the order. A transition between digits and characters is equivalent to a hyphen. Empty tokens are replaced with "0
". This gives a sequence of version numbers (numeric tokens) and version qualifiers (non-numeric tokens) with ".
" or "-
" prefixes)
拆分和替換示例:
-
1-1.foo-bar1baz-.1
->1-1.foo-bar-1-baz-0.1
然后,從版本的末尾開始,對后面的“null”值(0
,""
,"final
","ga
")進(jìn)行修剪。在每一個剩余的連字符上從頭到尾重復(fù)此過程。
修剪示例:
-
1.0.0
->1
-
1.ga
->1
-
1.final
->1
-
1.0
->1
-
1.
->1
-
1-
->1
-
1.0.0-foo.0.0
->1-foo
-
1.0.0-0.0.0
->1
版本順序是這個帶前綴的token序列上的“詞典順序”,帶有匹配前綴的較短token,填充了足夠多的的“null”值,與較長的token長度相同。填充的“null”值取決于其他版本的前綴:0表示“.”,"" 代表 '-'。帶前綴的token順序為:
-
如果前綴相同,則比較token:
-
字標(biāo)型token按自然順序排序。
-
非數(shù)字型token(“限定符”)按字母順序排序,除了下token按此順序排列在前:
"
alpha
" < "beta
" < "milestone
" < "rc
" = "cr
" < "snapshot
" < "" = "final
" = "ga
" < "sp
"- 當(dāng)其后直接跟數(shù)字時,"
alpha
", "beta
" 和"milestone
"可以分別縮寫為"a", "b"和"m"
- 當(dāng)其后直接跟數(shù)字時,"
-
-
否 "
.qualifier
" = "-qualifier
" < "-number
" < ".number
" -
alpha
=a
<<> = b
<<<milestone>> =m
<> = cr
<<<snapshot>> '<<<>>' =final
=ga
=release
<sp
建議遵循semver規(guī)則,不建議使用某些修飾:
- '
alpha
', 'beta
', 和'milestone
' 修飾符優(yōu)先于 'ea
' 和'preview
'. - '
1.0.0-RC1
'' 優(yōu)先于'1.0.0.RC1
'. - 不推薦使用 '
CR
'限定符,改用'RC
'。 - 不建議使用 '
final
', 'ga
'和'release
' 限定符 - 不建議使用 '
SP
' 限定符。增加補(bǔ)丁版本號(patch version)
最終結(jié)果示例:
-
"
1
" < "1.1
" (數(shù)字填充) -
"
1-snapshot
" < "1
" < "1-sp
" (限定符填充) -
"
1-foo2
" < "1-foo10
" (正確自動的"切換"為數(shù)字順序) -
"
1.foo
" = "1-foo
" < "1-1
" < "1.1
" -
"
1.ga
" = "1-ga
" = "1-0
" = "1.0
" = "1
" (移除末尾"null"值) -
"
1-sp
" > "1-ga
" -
"
1-sp.1
" > "1-ga.1
" -
"
1-sp-1
" < "1-ga-1
" = "1-1
" (每個連字符后面的"null"值) -
"
1-a1
" = "1-alpha-1
"注意:與一些設(shè)計文檔中所述的相反,對于版本順序,snapshot與release或任何其他限定符沒有區(qū)別對待。
注意:由于
2.0-rc1
<2.0
,版本要求[1.0,2.0)
不包括2.0
但包括2.0-rc1
,這與大多數(shù)人的預(yù)期相反。此外,Gradle對它的解釋不同,導(dǎo)致同一POM的依賴樹不同。如果打算將其限制為1.*版本,則更好的版本號要求是[1,1.9999999)
。
排除
限制依賴項的可傳遞依賴項有時很有用。依賴項可能具有錯誤指定的作用域,或者與項目中的其他依賴項沖突的依賴項。exclusions
告訴Maven不要在classpath中包含指定的工件,即使它是該項目的一個或多個依賴項的依賴項(傳遞依賴項)。例如, maven-embedder
依賴于maven-core
。假設(shè)您想依賴maven-embedder
,但不想在classpath中包含maven-core
或其依賴項,那么在聲明依賴 maven-embedder
的元素中添加maven-core
作為exclusion
:
<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>3.9.3</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
...
</project>
這只會從這個依賴項中刪除指向maven-core
的路徑。如果maven-core
在POM的其他地方作為直接或傳遞依賴出現(xiàn),那么它仍然可以添加到classpath徑中。
通配符排除,很容易排除依賴項的所有可傳遞依賴項。在以下情況中,假設(shè)你正在使用maven-embedder
,并且你希望管理你使用的依賴項,因此你排除了所有傳遞依賴項:
<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>3.8.6</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
...
</project>
-
exclusions:
exclusions
包含一個或多個exclusion
元素,每個元素都包含表示要排除的依賴項的groupId
和artifactId
。與可能安裝和使用,也可能不安裝和使用的optional
不同,exclusions
會主動從依賴樹中移除工件。
繼承
Maven為構(gòu)建管理帶來的一個強(qiáng)大的補(bǔ)充是項目繼承的概念。
<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>
</project>
Now we may add values to the parent POM, which will be inherited by its children. Most elements from the parent POM are inherited by its children, including
對于parent和aggregation(多模塊)項目,要求packaging
類型為pom
。這些類型定義了綁定到一組生命周期階段的目標(biāo)。例如,如果packaging
類型是jar
,那么packaging
階段將執(zhí)行jar:jar
目標(biāo)?,F(xiàn)在,我們可以為parent
添加值,該值將由其子代繼承。parent
的大多數(shù)元素由其子代繼承,包括:
- groupId
- version
- description
- url
- inceptionYear
- organization
- licenses
- developers
- contributors
- mailingLists
- scm
- issueManagement
- ciManagement
- properties
- dependencyManagement
- dependencies
- repositories
- pluginRepositories
- build
- plugin executions with matching ids
- plugin configuration
- etc.
- reporting
- profiles
值得注意是以下元素不被繼承:
-
artifactId
-
name
-
prerequisites
<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的一個意符,實現(xiàn)在搜索本地和遠(yuǎn)程倉庫之前,首先搜索為父項目提供的路徑,即relativePath
設(shè)置的值。
Super POM
類似于面向?qū)ο缶幊讨械膶ο罄^承,擴(kuò)展父POM的POM從該父POM繼承某些值。此外,正如Java對象最終繼承自java.lang.Object
一樣,所有項目對象模型都繼承自一個基本的Super POM。下面的片段是Maven 3.5.4的Super POM
<project>
<modelVersion>4.0.0</modelVersion>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>
<profiles>
<!-- NOTE: The release profile will be removed from future versions of the super POM -->
<profile>
<id>release-profile</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
你可以通過創(chuàng)建一個最小化的pom.xml
并在命令行上執(zhí)行:mvn help:effective-pom
來了解Super POM如何影響你的項目對象模型
依賴管理
除了繼承某些頂級元素外,parent
還擁有一些元素,可以為子POM和傳遞依賴項配置值。其中一個要素是dependencyManagement
。
-
dependencyManagement
:由POM用來幫助管理其所有子級的依賴關(guān)系信息。如果my-parent
使用dependencyManagement
定義對junit:junit:4.12
的依賴,那么從這個項目繼承的POM設(shè)置他們的依賴項時可以僅提供groupId=junit
和artifactId=junit
,version
將被Maven填充為父項目設(shè)置的版本。這種方法的好處是顯而易見的。可以集中在一個中心位置設(shè)置依賴關(guān)系詳細(xì)信息,并傳播到所有繼承的POM。請注意,從可傳遞依賴項合并的工件的版本和作用域也由依賴項管理部分中的版本規(guī)范控制。這可能會導(dǎo)致意想不到的后果。考慮一個項目使用兩個依賴項
dep1
和dep2
的情況。dep2
反過來也使用dep1,并且需要特定的最低版本才起作用。如果隨后使用dependencyManagement
指定較舊版本,dep2
將被迫使用較舊版本,因此失敗。因此,您必須小心檢查整個依賴樹,以避免出現(xiàn)此問題;mvn dependency:tree
很有幫助。
聚合(或多模塊)
A project with modules is known as a multi-module, or aggregator project. Modules are projects that this POM lists, and are executed as a group. A pom
packaged project may aggregate the build of a set of projects by listing them as modules, which are relative paths to the directories or the POM files of those projects.
包含模塊的項目稱為多模塊或聚合項目。模塊是本POM列出的項目,并作為一個組執(zhí)行。pom
打包項目可以通過將一系列項目列為模塊(項目目錄或pom文件的相對路徑)來聚合構(gòu)建。
<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>
在列出模塊時,你不需要自己考慮模塊間的依賴關(guān)系;也就是說,由POM給出的模塊的排序并不重要。Maven將對模塊進(jìn)行拓?fù)渑判?,這樣依賴關(guān)系總是在依賴模塊之前構(gòu)建。
屬性(Properties)
Properties are the last required piece to understand POM basics. Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}
, where X
is the property. Or they can be used by plugins as default values, for example:
properties
是理解POM基礎(chǔ)知識所需的最后一部分。Maven properties
是值占位符,類似于Ant中的properties
。通過使用符號${X}
,可以在POM中的任何位置訪問properties
的值,其中X
是property
?;蛟S它們可以被插件用作默認(rèn)值,例如:
<project>
...
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!-- Following project.-properties are reserved for Maven in will become elements in a future POM definition. -->
<!-- Don't start your own properties properties with project. -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
...
</project>
properties
具有5種不同的風(fēng)格:
-
env.X
:在屬性變量前加前綴env.
將返回shell環(huán)境變量。例如,${env.PATH}
將返回PATH
環(huán)境變量值。注意:雖然環(huán)境變量本身在Windows上不區(qū)分大小寫,但
properties
的查找是區(qū)分大小寫的。換句話說,雖然Windows shell為%PATH%
和%Path%
返回相同的值,但Maven區(qū)分${env.PATH}
和${env.Path}
。為了可靠性,將環(huán)境變量的名稱都標(biāo)準(zhǔn)化為大寫。 -
project.x
: POM中點分路徑將包含相應(yīng)元素的值。例如:通過${project.version}
獲取version
屬性值1.0
-
settings.x
:settings.xml
點分路徑將包含相應(yīng)元素的值。例如:通過${settings.offline}
獲取offline
屬性值false
-
Java系統(tǒng)屬性:所有可通過
Java.lang.System.getProperties()
訪問的屬性都可用作POM屬性,比如${java.home}
-
x
: 在POM中的元素內(nèi)設(shè)置。
<properties><someVar>value</someVar></properties>
的值value
可以用作${someVar}
。
構(gòu)建設(shè)置
Build
根據(jù)POM 4.0.0 XSD,build
元素在概念上分為兩個部分:一個是BaseBuild
類型,它包含兩個build
元素共有的一系列元素(project
下的頂級build
元素和profiles
下的build
元件,如下所述);另一個是Build
類型,包含BaseBuild
元素集以及用于頂層定義的更多元素。
注意:這些不同的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>
BaseBuild元素集
BaseBuild
:POM中兩個build
元素之間的基本元素集。
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
-
defaultGoal: 如果什么都沒有給定時,默認(rèn)執(zhí)行的目標(biāo)(goal)或階段(phase)。如果給定了一個目標(biāo),那么應(yīng)該和在命令行中一樣定義它(例如
jar:jar
)。如果定義了一個階段(例如install),情況也是如此。 -
directory: 這是構(gòu)建將轉(zhuǎn)儲其文件,或者用Maven的術(shù)語,其構(gòu)建目標(biāo)的目錄。它恰當(dāng)?shù)啬J(rèn)為
${basedir}/target
。 -
finalName: 這是綁定項目最終被構(gòu)建的名稱(沒有文件擴(kuò)展名,例如:
my-project-1.0.jar
)。默認(rèn)為${artifactId}-${version}
。然而,術(shù)語finalName
有點用詞不當(dāng),因為構(gòu)建綁定項目的插件完全有權(quán)忽略、修改這個名稱(通常不會)。例如,如果maven-jar-plugin
被配置為給某個jar一個test
的classifier
,那么上面定義的jar實際將被構(gòu)建為my-project-1.0-test.jar
。 -
filter:定義
*.properties
文件,該文件包含應(yīng)用于接受其設(shè)置的資源的屬性列表(如下所述)。換句話說,filter
文件中定義的 "name=value
"對將在構(gòu)建時替換資源中的${name}
字符串。上面的示例定義了位于filters/
目錄下的filter1.properties
文件。Maven的默認(rèn)filter目錄為${basedir}/src/main/filters/
。要更全面地了解filter是什么以及它們可以做什么,請查看快速入門指南
-
For a more comprehensive look at what filters are and what they can do, take a look at the quick start guide.
*.properties
files that contain a list of properties that apply to resources which accept their settings (covered below). In other words, the "
name=value
" pairs defined within the filter files replace
${name}
strings within resources on build. The example above defines the
filter1.properties
file under the
filters/
directory. Maven's default filter directory is
${basedir}/src/main/filters/
.
For a more comprehensive look at what filters are and what they can do, take a look at the quick start guide.
資源(Resources)
build
元素的另一個功能是指定項目中資源的位置。資源不是(通常)代碼。它們不被編譯,但是需要捆綁在項目中或用于其它各種需要(如代碼生成)。
例如,某個Plexus項目需要一個位于META-INF/plexus
目錄中的configuration.xml
文件(該文件指定容器的組件配置)。盡管我們可以很容易地將此文件放置在src/main/resources/META-INF/plexus
中,但我們希望為plexus提供自己的src/main/plexus
目錄。為了讓JAR插件正確地綁定資源,你可以指定類似于以下資源:
<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>
- resources: 是一個資源元素列表,每個元素都描述了包含與此項目相關(guān)聯(lián)的文件的內(nèi)容和位置。
-
targetPath: 指定用于放置來自構(gòu)建的資源集的目錄結(jié)構(gòu)。
targetPath
默認(rèn)為基目錄(base目錄)。通常為將打包在JAR中的資源指定的targetPath
為META-INF
。 -
filtering:
true
或者false
, 表示是否要為此資源啟用過濾。請注意,過濾器*.properties
文件不定義也可進(jìn)行過濾-資源也可以使用默認(rèn)情況下在POM中定義的properties
(如${project.version}
),使用-D
標(biāo)志(例如,"-Dname
=value
")傳遞到命令行或由properties
元素顯式定義的屬性。 -
directory:
${basedir}/src/main/resources
.此元素的值定義了資源的查找位置。構(gòu)建的默認(rèn)目錄是${basedir}/src/main/resources
。 -
includes: 指定要作為資源包含在指定目錄下的文件,使用
*
作為通配符 -
excludes: 與
includes
的結(jié)構(gòu)相同,不過用于指定要忽略的文件。如果include
和exclude
之間如果存在沖突,則exclude
“獲勝”。 -
testResources:
testResources
元素塊包含testResource
元素。它們的定義類似于resource
元素,不過是在測試階段使用。一個區(qū)別是,項目的默認(rèn)(Super POM定義的)測試資源目錄是${basedir}/src/test/resources
。測試資源不被發(fā)布。
插件(Plugins)
<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>
除了標(biāo)準(zhǔn)坐標(biāo) groupId:artifactId:version
之外,還有一些元素可以配置插件或與插件交互的構(gòu)建。
-
extensions:
true
或者false
,是否加載此插件的擴(kuò)展。默認(rèn)情況下為false
。 -
inherited:
true
或者false
,這個插件配置是否應(yīng)該應(yīng)用于繼承自這個插件的POM。默認(rèn)true
. -
configuration: 這是特定于單個插件的。插件Mojo可能期望的任何屬性(這些是Java Mojo bean中的getter和setter)都可以在這里指定。在上面的例子中,我們將
maven-jar-plugin
Mojo中classifier
屬性設(shè)置為test。值得注意的是,所有配置元素,無論它們在POM中的哪個位置,都旨在將值傳遞給另一個底層系統(tǒng),例如插件。換句話說:POM模式從來沒有明確要求configuration
元素中的值,但插件目標(biāo)完全有權(quán)要求configuration
值。如果你的POM聲明了一個parent,它將從parent的
build/plugins
或pluginManagement
部分繼承插件配置。-
默認(rèn)配置繼承:
為了進(jìn)行說明,假設(shè)有來自父POM的以下配置:
<plugin> <groupId>my.group</groupId> <artifactId>my-plugin</artifactId> <configuration> <items> <item>parent-1</item> <item>parent-2</item> </items> <properties> <parentKey>parent</parentKey> </properties> </configuration> </plugin>
假設(shè)以下為某個項目的插件配置,該項目使用上述POM作為其parent
<plugin> <groupId>my.group</groupId> <artifactId>my-plugin</artifactId> <configuration> <items> <item>child-1</item> </items> <properties> <childKey>child</childKey> </properties> </configuration> </plugin>
默認(rèn)行為是根據(jù)元素名稱合并configuration 元素的內(nèi)容。如果子POM具有特定元素,則其值將成為有效值。如果子POM沒有元素,但父POM有,則父值將成為有效值。請注意,這純粹是對XML的操作;不涉及插件本身的代碼或配置,只涉及元素,而不是它們的值。
將這些規(guī)則應(yīng)用到示例中,得到以下配置:
<plugin> <groupId>my.group</groupId> <artifactId>my-plugin</artifactId> <configuration> <items> <item>child-1</item> </items> <properties> <childKey>child</childKey> <parentKey>parent</parentKey> </properties> </configuration> </plugin>
-
高級配置繼承:combine.children 和combine.self
可以通過向
configuration
元素的子元素添加屬性--combine.children
和combing.self
,來控制子POM如何從父POM繼承配置。在子POM中使用這些屬性可以控制Maven如何將父級的插件配置與子級的顯式配置相結(jié)合。以下是帶有這兩個屬性示例的子POM配置
<configuration> <items combine.children="append"> <!-- combine.children="merge" is the default --> <item>child-1</item> </items> <properties combine.self="override"> <!-- combine.self="merge" is the default --> <childKey>child</childKey> </properties> </configuration>
現(xiàn)在,有效的結(jié)果如下:
<configuration> <items combine.children="append"> <item>parent-1</item> <item>parent-2</item> <item>child-1</item> </items> <properties combine.self="override"> <childKey>child</childKey> </properties> </configuration>
combine.children="append"
將按順序連接父元素和子元素。而combine.self="override"
則完全抑制父配置。不能在元素上同時使用combine.self="override"
和combine.children="append"
,如果同時配置了則combine.self="override"
。注意,這些屬性只應(yīng)用于它們聲明的配置元素,而不會傳遞到嵌套元素。也就是說,如果子POM中的
item
元素的內(nèi)容是一個復(fù)雜的結(jié)構(gòu),而不是文本,那么它的子元素仍將受到默認(rèn)合并策略的約束,除非它們本身用屬性標(biāo)記。子POM會從父POM繼承
combine.*
屬性。將這些屬性添加到父POM時要小心,因為這可能會影響子POM或子孫POM。
-
-
dependencies: 在POM中可以看到很多依賴項,它們是所有
plugins
元素塊下的一個元素。依賴項具有與basebuild
下相同的結(jié)構(gòu)和功能。這種情況下的主要區(qū)別在于,它們不再作為項目的依賴項應(yīng)用,而是作為所屬插件的依賴項來應(yīng)用。這樣做的功能是更改插件的依賴項列表,可能是通過exclusions
刪除未使用的運行時依賴項,或者更改所需依賴項的版本。 -
executions:記住,一個插件可能有多個目標(biāo)。每個目標(biāo)可能有一個單獨的配置,甚至可能將插件的目標(biāo)綁定到不同的階段
executions
配置插件目標(biāo)的execution
。例如,假設(shè)你想將
antrun:run
目標(biāo)綁定到verify
階段。我們希望任務(wù)回顯構(gòu)建目錄,并通過將inherited
設(shè)置為false
來避免將此配置傳遞給其子級(假設(shè)它是父級)。你將會得到這樣的execution
:<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: /home/jenkins/82467a7c/workspace/aven_maven-box_maven-site_master/target</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
-
id: 此
execution
塊唯一標(biāo)識。當(dāng)階段運行時,它將以[plugin:target-execution:id]
的形式顯示。在本例中:[antrun:run execution:echodir]
-
goals: 包含一個單數(shù)元素(
goal
)列表。在這種情況下,由這個execution
塊指定的插件goals
列表 -
phase:設(shè)置目標(biāo)列表將在其中執(zhí)行的階段。這是一個非常強(qiáng)大的選項,允許將任何目標(biāo)綁定到構(gòu)建生命周期中的任何階段,從而改變Maven的默認(rèn)行為
-
inherited: 類似上面的
inherited
元素,設(shè)置為false
將禁止Maven將此execution
傳遞給其子級。此元素僅對父POM有意義 -
configuration: 與上面的
configuration
相同,除了將配置限制在這個特定的目標(biāo)列表中,而不是插件下的所有目標(biāo)
-
插件管理(Plugin Management)
-
pluginManagement: 插件管理以和上文
plugins
幾乎相同的方式包含插件元素,只是它不是為這個特定的項目構(gòu)建配置插件信息,而是旨在配置從這個項目構(gòu)建繼承的項目構(gòu)建。然而,這只配置在子POM或當(dāng)前POM中plugins
元素實際引用的插件。子POM們完全有權(quán)覆蓋pluginManagement
定義。
<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>
...
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>pre-process-classes</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>pre-process</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
...
</build>
</project>
如果我們將這些規(guī)范添加到plugins
元素中,它們將僅適用于單個POM。然而,如果我們在pluginManagement
元素下應(yīng)用它們,那么這個POM和所有將maven-jar-plugin
添加到構(gòu)建中的繼承POM也將獲取 pre-process-classes
execution
。因此,與其在每個子pom.xml
中包含上述的混亂,只需要以下內(nèi)容::
<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>
</plugin>
</plugins>
...
</build>
</project>
Build元素集(The Build Element Set)
目錄(Directories)
目錄元素集位于父級build
元素中,該元素作為一個整體為POM設(shè)置了各種目錄結(jié)構(gòu)。由于它們不存在于profiles
build
中,因此profiles
無法更改這些內(nèi)容。
<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>
如果上面*Directory
元素的值設(shè)置為絕對路徑,則使用該路徑。否則使用相對于基構(gòu)建目錄:${basedir}
的路徑。注意,scriptSourceDirectory
未在Maven中使用,并且已經(jīng)過時。
擴(kuò)展(Extensions)
擴(kuò)展為要在此構(gòu)建中使用的工件列表。它們將包含在正在運行的構(gòu)建的classpath中。它們可以對構(gòu)建過程開啟擴(kuò)展(例如為Wagon傳輸機(jī)制添加ftp提供商),并使插件處于活動狀態(tài),從而更改構(gòu)建生命周期。簡而言之,擴(kuò)展是在構(gòu)建過程中激活的工件。擴(kuò)展實際上不必做任何事情,也不必包含Mojo。因此,擴(kuò)展非常適合指定通用插件接口的多個實現(xiàn)中的一個。
<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>
報告(Reporting)
<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>
<outputDirectory>${basedir}/target/site</outputDirectory>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.0.1</version>
<reportSets>
<reportSet></reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
...
</project>
......
更多配置參考,請查閱官方文檔文章來源:http://www.zghlxwxcb.cn/news/detail-665590.html
參考鏈接
https://maven.apache.org/pom.html文章來源地址http://www.zghlxwxcb.cn/news/detail-665590.html
到了這里,關(guān)于Java Maven POM配置參考的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!