maven setting 文件講解

目錄

概要

??settings.xml有什么用?

??settings.xml文件位置

??配置優(yōu)先級(jí)

settings.xml元素詳解

??頂級(jí)元素概覽

??LocalRepository

??InteractiveMode

??UsePluginRegistry

??Offline

??PluginGroups

??Servers

??Mirrors

??Proxies

??Profiles

????Activation

????properties

????Repositories

????pluginRepositories

??ActiveProfiles

更多閱讀

概要

settings.xml有什么用?

如果在Eclipse中使用過Maven插件,想必會(huì)有這個(gè)經(jīng)驗(yàn):配置settings.xml文件的路徑。

settings.xml文件是干什么的,為什么要配置它呢?

從settings.xml的文件名就可以看出,它是用來設(shè)置maven參數(shù)的配置文件。并且,settings.xml是maven的全局配置文件。而pom.xml文件是所在項(xiàng)目的局部配置。

Settings.xml中包含類似本地倉儲(chǔ)位置、修改遠(yuǎn)程倉儲(chǔ)服務(wù)器、認(rèn)證信息等配置。

settings.xml文件位置

settings.xml文件一般存在于兩個(gè)位置:

全局配置: ${M2_HOME}/conf/settings.xml

用戶配置: user.home/.m2/settings.xmlnote:用戶配置優(yōu)先于全局配置。{user.home} 和和所有其他系統(tǒng)屬性只能在3.0+版本上使用。請(qǐng)注意windows和Linux使用變量的區(qū)別。

配置優(yōu)先級(jí)

需要注意的是:局部配置優(yōu)先于全局配置。

配置優(yōu)先級(jí)從高到低:pom.xml> user settings > global settings

如果這些文件同時(shí)存在,在應(yīng)用配置時(shí),會(huì)合并它們的內(nèi)容,如果有重復(fù)的配置,優(yōu)先級(jí)高的配置會(huì)覆蓋優(yōu)先級(jí)低的。

settings.xml元素詳解

頂級(jí)元素概覽

下面列舉了settings.xml中的頂級(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/> <usePluginRegistry/> <offline/> <pluginGroups/> <servers/> <mirrors/> <proxies/> <profiles/> <activeProfiles/> </settings>

LocalRepository

作用:該值表示構(gòu)建系統(tǒng)本地倉庫的路徑。

其默認(rèn)值:~/.m2/repository。

<localRepository>${user.home}/.m2/repository</localRepository>

InteractiveMode

作用:表示maven是否需要和用戶交互以獲得輸入。

如果maven需要和用戶交互以獲得輸入,則設(shè)置成true,反之則應(yīng)為false。默認(rèn)為true。

<interactiveMode>true</interactiveMode>

UsePluginRegistry

作用:maven是否需要使用plugin-registry.xml文件來管理插件版本。

如果需要讓maven使用文件~/.m2/plugin-registry.xml來管理插件版本,則設(shè)為true。默認(rèn)為false。

<usePluginRegistry>false</usePluginRegistry>

Offline

作用:表示maven是否需要在離線模式下運(yùn)行。

如果構(gòu)建系統(tǒng)需要在離線模式下運(yùn)行,則為true,默認(rèn)為false。

當(dāng)由于網(wǎng)絡(luò)設(shè)置原因或者安全因素,構(gòu)建服務(wù)器不能連接遠(yuǎn)程倉庫的時(shí)候,該配置就十分有用。

<offline>false</offline>

PluginGroups

作用:當(dāng)插件的組織id(groupId)沒有顯式提供時(shí),供搜尋插件組織Id(groupId)的列表。

該元素包含一個(gè)pluginGroup元素列表,每個(gè)子元素包含了一個(gè)組織Id(groupId)。

當(dāng)我們使用某個(gè)插件,并且沒有在命令行為其提供組織Id(groupId)的時(shí)候,Maven就會(huì)使用該列表。默認(rèn)情況下該列表包含了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.codehaus.mojo</pluginGroup> </pluginGroups> ... </settings>

Servers

作用:一般,倉庫的下載和部署是在pom.xml文件中的repositories和distributionManagement元素中定義的。然而,一般類似用戶名、密碼(有些倉庫訪問是需要安全認(rèn)證的)等信息不應(yīng)該在pom.xml文件中配置,這些信息可以配置在settings.xml中。

<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>${usr.home}/.ssh/id_dsa</privateKey> <passphrase>some_passphrase</passphrase> <filePermissions>664</filePermissions> <directoryPermissions>775</directoryPermissions> </server> </servers> ... </settings>

Mirrors

作用:為倉庫列表配置的下載鏡像列表。

<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>

Proxies

作用:用來配置不同的代理。

<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

作用:根據(jù)環(huán)境參數(shù)來調(diào)整構(gòu)建配置的列表。

settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。

它包含了id、activation、repositories、pluginRepositories和 properties元素。這里的profile元素只包含這五個(gè)子元素是因?yàn)檫@里只關(guān)心構(gòu)建系統(tǒng)這個(gè)整體(這正是settings.xml文件的角色定位),而非單獨(dú)的項(xiàng)目對(duì)象模型設(shè)置。如果一個(gè)settings.xml中的profile被激活,它的值會(huì)覆蓋任何其它定義在pom.xml中帶有相同id的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> <id>test</id> <activation /> <properties /> <repositories /> <pluginRepositories /> </profile> </profiles> ... </settings>

Activation

作用:自動(dòng)觸發(fā)profile的條件邏輯。

如pom.xml中的profile一樣,profile的作用在于它能夠在某些特定的環(huán)境中自動(dòng)使用某些特定的值;這些環(huán)境通過activation元素指定。

activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id。profile也可以通過在命令行,使用-P標(biāo)記和逗號(hào)分隔的列表來顯式的激活(如,-P test)。

<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>

注:在maven工程的pom.xml所在目錄下執(zhí)行mvn help:active-profiles命令可以查看中央倉儲(chǔ)的profile是否在工程中生效。

properties

作用:對(duì)應(yīng)profile的擴(kuò)展屬性列表。

maven屬性和ant中的屬性一樣,可以用來存放一些值。這些值可以在pom.xml中的任何地方使用標(biāo)記${X}來使用,這里X是指屬性的名稱。屬性有五種不同的形式,并且都能在settings.xml文件中訪問。

<properties> <user.install>${user.home}/our-project</user.install> </properties>

注:如果該profile被激活,則可以在pom.xml中使用${user.install}。

Repositories

作用:遠(yuǎn)程倉庫列表,它是maven用來填充構(gòu)建系統(tǒng)本地倉庫所使用的一組遠(yuǎn)程倉庫。

<repositories> <repository> <id>codehausSnapshots</id> <name>Codehaus Snapshots</name> <releases> <enabled>false</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots> <url>http://snapshots.maven.codehaus.org/maven2</url> <layout>default</layout> </repository> </repositories>

pluginRepositories

作用:發(fā)現(xiàn)插件的遠(yuǎn)程倉庫列表。

和repository類似,只是repository是管理jar包依賴的倉庫,pluginRepositories則是管理插件的倉庫。

maven插件是一種特殊類型的構(gòu)件。由于這個(gè)原因,插件倉庫獨(dú)立于其它倉庫。pluginRepositories元素的結(jié)構(gòu)和repositories元素的結(jié)構(gòu)類似。每個(gè)pluginRepository元素指定一個(gè)Maven可以用來尋找新插件的遠(yuǎn)程地址。

<pluginRepositories> <pluginRepository> <releases> <enabled /> <updatePolicy /> <checksumPolicy /> </releases> <snapshots> <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots> <id /> <name /> <url /> <layout /> </pluginRepository> </pluginRepositories>

ActiveProfiles

作用:手動(dòng)激活profiles的列表,按照profile被應(yīng)用的順序定義activeProfile。

該元素包含了一組activeProfile元素,每個(gè)activeProfile都含有一個(gè)profile id。任何在activeProfile中定義的profile id,不論環(huán)境設(shè)置如何,其對(duì)應(yīng)的 profile都會(huì)被激活。如果沒有匹配的profile,則什么都不會(huì)發(fā)生。

例如,env-test是一個(gè)activeProfile,則在pom.xml(或者profile.xml)中對(duì)應(yīng)id的profile會(huì)被激活。如果運(yùn)行過程中找不到這樣一個(gè)profile,Maven則會(huì)像往常一樣運(yùn)行。

<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>

至此,maven settings.xml中的標(biāo)簽都講解完畢,希望對(duì)大家有所幫助。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容