本文是對(duì)https://maven.apache.org/settings.html的翻譯和總結(jié)
介紹
快速概覽
settings文件是一個(gè)全局的配置,并不與某一個(gè)項(xiàng)目綁定。它包含了對(duì)本地庫(kù)位置的配置,遠(yuǎn)程庫(kù)服務(wù),驗(yàn)證信息。
settings.xml可以配置在兩個(gè)位置:
- maven的安裝目錄
- 用戶目錄:
${user.home}/.m2/settings.xml
maven安裝目錄中的settings.xml是一個(gè)全局的配置,用戶目錄中的settings.xml文件只對(duì)當(dāng)前用戶有效。
如果兩個(gè)文件都存在,則其內(nèi)容將合并,用戶特定的settings.xml占主導(dǎo)地位。
以下是settings下的頂級(jí)元素的概述:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
可以使用以下表達(dá)式插入settings.xml的內(nèi)容:
- ${user.home} 和其他所有系統(tǒng)屬性 (since Maven 3.0)
- ${env.HOME} 等環(huán)境變量
請(qǐng)注意,
settings.xml中的<profiles>元素中定義的properties不能用于插值。
settings 細(xì)節(jié)
簡(jiǎn)單值
一半的頂級(jí)元素是簡(jiǎn)單值,表示一系列值,用于描述構(gòu)建系統(tǒng)中全程有效的元素。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${user.home}/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
...
</settings>
-
localRepository:本地倉(cāng)庫(kù)的地址,默認(rèn)是
${user.home}/.m2/repository - interactiveMode:是否以交互式進(jìn)行輸入,則為true,否則為false。默認(rèn)為true。例如在創(chuàng)建maven應(yīng)用上時(shí)以交互式創(chuàng)建。
1. 非交互式創(chuàng)建應(yīng)用:
mvn archetype:generate
-DgroupId=com.howtodoinjava
-DartifactId=DemoJavaProject
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
2. 交互式創(chuàng)建maven應(yīng)用
mvn archetype:generate
此時(shí),maven會(huì)讓你輸入項(xiàng)目的一些配置,例如groupId,artifactId,version等
- offline:是否在offline模式下操作。由于網(wǎng)絡(luò)設(shè)置或安全原因,此元素對(duì)無(wú)法連接到遠(yuǎn)程存儲(chǔ)庫(kù)的構(gòu)建服務(wù)器很有用。
Plugin Groups
該元素包含一個(gè)pluginGroup元素列表,每個(gè)元素都包含一個(gè)groupId。使用插件時(shí)搜索列表,并且命令行中未提供groupId。此列表自動(dòng)包含org.apache.maven.plugins和org.codehaus.mojo。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<pluginGroups>
<pluginGroup>org.eclipse.jetty</pluginGroup>
</pluginGroups>
...
</settings>
例如,給定上述設(shè)置,Maven命令行可以以簡(jiǎn)寫形式執(zhí)行org.eclipse.jetty:jetty-maven-plugin:run:
mvn jetty:run
Servers
用于下載和部署的存儲(chǔ)庫(kù)由POM的repositories和distributionManagement元素定義。但是,某些設(shè)置(如用??戶名和密碼)不應(yīng)與pom.xml一起分發(fā)。此類信息應(yīng)存在于settings.xml中的構(gòu)建服務(wù)器上。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<servers>
<server>
<id>server001</id>
<username>my_login</username>
<password>my_password</password>
<privateKey>${user.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</servers>
...
</settings>
- id:倉(cāng)庫(kù)的名稱
- username,password:這些元素顯示為一對(duì),表示對(duì)此服務(wù)器進(jìn)行身份驗(yàn)證所需的登錄名和密碼。
-
privateKey, passphrase:這也是成對(duì)出現(xiàn),表示私鑰的路徑(默認(rèn)是
${user.home}/.ssh/id_dsa)和密碼。密碼和密碼元素將來(lái)可能會(huì)外部化,但是現(xiàn)在必須在settings.xml文件中設(shè)置純文本。 - filePermissions, directoryPermissions:在部署時(shí)創(chuàng)建存儲(chǔ)庫(kù)文件或目錄時(shí),這些是要使用的權(quán)限。每個(gè)的合法值是對(duì)應(yīng)于* nix文件權(quán)限的三位數(shù)字,例如, 664或775。
注意:如果使用私鑰登錄服務(wù)器,請(qǐng)確保省略
<password>元素。否則,密鑰將被忽略。
密碼加密
一個(gè)新功能 - 服務(wù)器密碼和密碼加密已添加到2.1.0+。查看此頁(yè)面的詳細(xì)信息
MIrrors
使用鏡像的一些原因:
- 互聯(lián)網(wǎng)上有一個(gè)同步鏡像,地理位置更近,速度更快
- 您希望將特定存儲(chǔ)庫(kù)替換為您可以更好地控制的內(nèi)部存儲(chǔ)庫(kù)
- 您希望運(yùn)行存儲(chǔ)庫(kù)管理器以向鏡像提供本地緩存,并且需要使用其URL
一般是第一個(gè)原因
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<mirrors>
<mirror>
<id>planetmirror.com</id>
<name>PlanetMirror Australia</name>
<url>http://downloads.planetmirror.com/pub/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
...
</settings>
-
id,name:id必須唯一(不要與倉(cāng)庫(kù)
id一樣),name隨意 - url:鏡像的地址
-
mirrorOf:需要被鏡像的倉(cāng)庫(kù)
id。例如,要指向Maven中央存儲(chǔ)庫(kù)(https://repo.maven.apache.org/maven2/)的鏡像,請(qǐng)將此元素設(shè)置為central。更高級(jí)的映射,如repo1,repo2或*,!inhouse也是可能的。這必須與鏡像ID不匹配。
有關(guān)鏡像的更深入介紹,請(qǐng)閱讀鏡像設(shè)置指南。
Proxies
如果你訪問(wèn)不了某一個(gè)倉(cāng)庫(kù),可以使用代理,用的不多,所以這里也不詳細(xì)說(shuō)明。想要了解的可以去官網(wǎng)查看。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
</proxy>
</proxies>
...
</settings>
Profiles
settings.xml中的profile元素是pom.xml profile元素的精簡(jiǎn)版本。它由activation,repositories,pluginRepositories和properties元素組成。概要文件元素僅包含這四個(gè)元素,因?yàn)樗鼈冴P(guān)注整個(gè)構(gòu)建系統(tǒng)(這是settings.xml文件的作用),而不是單個(gè)項(xiàng)目對(duì)象模型設(shè)置。
如果profile在settings.xml中被激活,則其值將覆蓋POM或profiles.xml文件中任何等效的ID'd配置。
Activation
該屬性可以配置配置文件的激活條件,例如,激活條件必須是 jdk8,必須是windows等。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.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>mavenVersion</name>
<value>2.0.3</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
...
</profile>
</profiles>
...
</settings>
當(dāng)滿足所有指定條件時(shí)激活,但不是一次都需要。
-
jdk:activation在jdk元素中有一個(gè)內(nèi)置的,以Java為中心的檢查。如果測(cè)試是在與給定前綴匹配的jdk版本號(hào)下運(yùn)行,則會(huì)激活此選項(xiàng)。在上面的例子中,
1.5.0_06將匹配。從Maven 2.1開(kāi)始也支持范圍。有關(guān)支持的范圍的更多詳細(xì)信息,請(qǐng)參閱maven-enforcer-plugin。 - os:os元素可以定義上面顯示的一些操作系統(tǒng)特定屬性。有關(guān)OS值的更多詳細(xì)信息,請(qǐng)參閱maven-enforcer-plugin。
-
property:如果Maven檢測(cè)到相應(yīng)
name=value對(duì)的屬性(可以在POM中取消引用${name}的值),則將激活該配置文件。 -
file:最后,給定的文件名可以通過(guò)存在文件來(lái)激活配置文件,或者如果它丟失了。
activation元素不是激活配置文件的唯一方式。settings.xml文件的activeProfile元素可能包含配置文件的id。它們也可以在-P標(biāo)志(例如-P test)之后通過(guò)命令行通過(guò)逗號(hào)分隔列表顯式激活。
要查看將在特定版本中激活的配置文件,請(qǐng)使用maven-help-plugin。
mvn help:active-profiles
Properties
Maven屬性是值占位符,就像Ant中的屬性一樣??梢允褂梅?hào)${X}在POM內(nèi)的任何位置訪問(wèn)它們的值,其中X是屬性。它們有五種不同的樣式,都可以從settings.xml文件中訪問(wèn):
-
env.X:使用“env.”前綴變量將返回shell的環(huán)境變量。例如,${env.PATH}返回$path環(huán)境變量(Windows中是%PATH%) -
project.x:POM中的點(diǎn)(.)標(biāo)記路徑將包含相應(yīng)元素的值。例如:<project><version>1.0</version></project>可通過(guò)${project.version}訪問(wèn)。 -
settings.x:settings.xml中的點(diǎn)(.)標(biāo)記路徑將包含相應(yīng)元素的值。例如:<settings><offline>false</offline></settings>可通過(guò)${settings.offline}訪問(wèn)。 - Java系統(tǒng)屬性:可通過(guò)
java.lang.System.getProperties()訪問(wèn)的所有屬性都可用作POM屬性,例如${java.home}。 - x:在
<properties />元素或外部文件中設(shè)置的值,該值可以用作${someVar}。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<profiles>
<profile>
...
<properties>
<user.install>${user.home}/our-project</user.install>
</properties>
...
</profile>
</profiles>
...
</settings>
如果此配置文件被啟用,則可以在POM中使用${user.install}訪問(wèn)該屬性。
Repositories
Repositories是Maven用于填充構(gòu)建系統(tǒng)的本地存儲(chǔ)庫(kù)的項(xiàng)目的遠(yuǎn)程集合。它來(lái)自這個(gè)本地存儲(chǔ)庫(kù),Maven稱之為插件和依賴項(xiàng)。不同的遠(yuǎn)程存儲(chǔ)庫(kù)可能包含不同的項(xiàng)目,并且在啟用的profile下,可以搜索它們以查找匹配的版本或快照工件。
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<profiles>
<profile>
...
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
...
</pluginRepositories>
...
</profile>
</profiles>
...
</settings>
- releases, snapshots:這是針對(duì)每種工件,Release或snapshot的策略。通過(guò)這兩個(gè)集合,POM可以在單個(gè)存儲(chǔ)庫(kù)中獨(dú)立于另一個(gè)類型更改每種類型的策略。例如,對(duì)于開(kāi)發(fā)目的的遠(yuǎn)程庫(kù),可以決定僅啟用快照下載。
-
enabled:是否為相應(yīng)類型(
releases或snapshots)啟用此存儲(chǔ)庫(kù),是true還是false。 -
updatePolicy:此元素指定更新本地庫(kù)頻率。Maven會(huì)將本地POM的時(shí)間戳(存儲(chǔ)在存儲(chǔ)庫(kù)的maven-metadata文件中)與遠(yuǎn)程數(shù)據(jù)進(jìn)行比較。選項(xiàng)包括:
always,daily(默認(rèn)),interva:X(其中X是以分鐘為單位的整數(shù))或never。 -
checksumPolicy:當(dāng)Maven將文件部署到存儲(chǔ)庫(kù)時(shí),它還會(huì)部署相應(yīng)的校驗(yàn)和文件。您可以選擇
ignore,fail或warn缺少或不正確的校驗(yàn)和。 - layout:。。。
Plugin Repositories
存儲(chǔ)庫(kù)是兩種主要類型的工件的所在地。第一個(gè)是用作其他工件的依賴項(xiàng)的工件。這些是位于中央的大多數(shù)插件。另一種類型的工件是插件。Maven插件本身就是一種特殊的工件。因此,插件存儲(chǔ)庫(kù)可能與其他存儲(chǔ)庫(kù)分離(盡管如此,我還沒(méi)有聽(tīng)到令人信服的論據(jù))。在任何情況下,pluginRepositories元素塊的結(jié)構(gòu)類似于repositories元素。 pluginRepository元素指定Maven可以在哪里找到新插件的遠(yuǎn)程位置。
啟用Profiles
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<activeProfiles>
<activeProfile>env-test</activeProfile>
</activeProfiles>
</settings>
settings.xml拼圖的最后一部分是activeProfiles元素。它包含一組activeProfile元素,包含一個(gè)profile的id。無(wú)論任何環(huán)境設(shè)置如何,任何定義為activeProfile的profile id都將將被啟用。如果沒(méi)有找到匹配的profile,則不會(huì)發(fā)生任何事。
詳細(xì)的maven屬性說(shuō)明
repository和pluginRepository的順序會(huì)影響獲取遠(yuǎn)程工建的順序鏡像匹配規(guī)則,首先找到完全匹配的鏡像,如果未找到完全匹配的鏡像,但是有多個(gè)間接匹配(符合條件),則會(huì)選擇第一個(gè)鏡像作為鏡像。
maven拉取jar的過(guò)程:
