目錄
概要
settings.xml有什么用?
如果在Eclipse中使用過Maven插件,想必會有這個經驗:配置settings.xml文件的路徑。
settings.xml文件是干什么的,為什么要配置它呢?
從settings.xml的文件名就可以看出,它是用來設置maven參數的配置文件。并且,settings.xml是maven的全局配置文件。而pom.xml文件是所在項目的局部配置。
Settings.xml中包含類似本地倉儲位置、修改遠程倉儲服務器、認證信息等配置。
settings.xml文件位置
settings.xml文件一般存在于兩個位置:
全局配置: ${M2_HOME}/conf/settings.xml
用戶配置: user.home/.m2/settings.xmlnote:用戶配置優先于全局配置。{user.home} 和和所有其他系統屬性只能在3.0+版本上使用。請注意windows和Linux使用變量的區別。
配置優先級
需要注意的是:局部配置優先于全局配置。
配置優先級從高到低:pom.xml> user settings > global settings
如果這些文件同時存在,在應用配置時,會合并它們的內容,如果有重復的配置,優先級高的配置會覆蓋優先級低的。
settings.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"> <localRepository/> <interactiveMode/> <usePluginRegistry/> <offline/> <pluginGroups/> <servers/> <mirrors/> <proxies/> <profiles/> <activeProfiles/> </settings>
LocalRepository
作用:該值表示構建系統本地倉庫的路徑。
其默認值:~/.m2/repository。
<localRepository>${user.home}/.m2/repository</localRepository>
InteractiveMode
作用:表示maven是否需要和用戶交互以獲得輸入。
如果maven需要和用戶交互以獲得輸入,則設置成true,反之則應為false。默認為true。
<interactiveMode>true</interactiveMode>
UsePluginRegistry
作用:maven是否需要使用plugin-registry.xml文件來管理插件版本。
如果需要讓maven使用文件~/.m2/plugin-registry.xml來管理插件版本,則設為true。默認為false。
<usePluginRegistry>false</usePluginRegistry>
Offline
作用:表示maven是否需要在離線模式下運行。
如果構建系統需要在離線模式下運行,則為true,默認為false。
當由于網絡設置原因或者安全因素,構建服務器不能連接遠程倉庫的時候,該配置就十分有用。
<offline>false</offline>
PluginGroups
作用:當插件的組織id(groupId)沒有顯式提供時,供搜尋插件組織Id(groupId)的列表。
該元素包含一個pluginGroup元素列表,每個子元素包含了一個組織Id(groupId)。
當我們使用某個插件,并且沒有在命令行為其提供組織Id(groupId)的時候,Maven就會使用該列表。默認情況下該列表包含了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元素中定義的。然而,一般類似用戶名、密碼(有些倉庫訪問是需要安全認證的)等信息不應該在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
作用:根據環境參數來調整構建配置的列表。
settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。
它包含了id、activation、repositories、pluginRepositories和 properties元素。這里的profile元素只包含這五個子元素是因為這里只關心構建系統這個整體(這正是settings.xml文件的角色定位),而非單獨的項目對象模型設置。如果一個settings.xml中的profile被激活,它的值會覆蓋任何其它定義在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
作用:自動觸發profile的條件邏輯。
如pom.xml中的profile一樣,profile的作用在于它能夠在某些特定的環境中自動使用某些特定的值;這些環境通過activation元素指定。
activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id。profile也可以通過在命令行,使用-P標記和逗號分隔的列表來顯式的激活(如,-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所在目錄下執行mvn help:active-profiles命令可以查看中央倉儲的profile是否在工程中生效。
properties
作用:對應profile的擴展屬性列表。
maven屬性和ant中的屬性一樣,可以用來存放一些值。這些值可以在pom.xml中的任何地方使用標記${X}來使用,這里X是指屬性的名稱。屬性有五種不同的形式,并且都能在settings.xml文件中訪問。
<properties> <user.install>${user.home}/our-project</user.install> </properties>
注:如果該profile被激活,則可以在pom.xml中使用${user.install}。
Repositories
作用:遠程倉庫列表,它是maven用來填充構建系統本地倉庫所使用的一組遠程倉庫。
<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
作用:發現插件的遠程倉庫列表。
和repository類似,只是repository是管理jar包依賴的倉庫,pluginRepositories則是管理插件的倉庫。
maven插件是一種特殊類型的構件。由于這個原因,插件倉庫獨立于其它倉庫。pluginRepositories元素的結構和repositories元素的結構類似。每個pluginRepository元素指定一個Maven可以用來尋找新插件的遠程地址。
<pluginRepositories> <pluginRepository> <releases> <enabled /> <updatePolicy /> <checksumPolicy /> </releases> <snapshots> <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots> <id /> <name /> <url /> <layout /> </pluginRepository> </pluginRepositories>
ActiveProfiles
作用:手動激活profiles的列表,按照profile被應用的順序定義activeProfile。
該元素包含了一組activeProfile元素,每個activeProfile都含有一個profile id。任何在activeProfile中定義的profile id,不論環境設置如何,其對應的 profile都會被激活。如果沒有匹配的profile,則什么都不會發生。
例如,env-test是一個activeProfile,則在pom.xml(或者profile.xml)中對應id的profile會被激活。如果運行過程中找不到這樣一個profile,Maven則會像往常一樣運行。
<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中的標簽都講解完畢,希望對大家有所幫助。