概述
Maven的settings.xml配置了Maven執行的方式,像pom.xml一樣,但是它是一個通用的配置, 不能綁定到任何特殊的項目。它通常包括本地倉庫地址,遠程倉庫服務,認證信息等。
settings.xml存在於兩個位置:
- maven目錄下的
/conf/settings.xml
- 用戶目錄下的
/.m2/settings.xml
maven目錄下的稱為全局配置,用戶目錄下的稱為用戶配置。如果兩個配置都存在,它們的內容將合並,有沖突的以用戶配置優先。 通常情況下,用戶目錄下的/.m2/settings.xml
是不存在的,如果你需要,可以從maven目錄下的/conf/settings.xml
復制過來。 maven的默認settings模板中,包含了所有的配置的例子,它們都被注釋掉了,如果你需要,可以打開注釋,配置你自己的信息。
下面是settings文件的頂層元素:
<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>
settings文件中的內容可以使用插值替換,例如:
${user.home}
或者其他的系統屬性(3.0以上)${env.HOME}
等環境變量
注意:profile中定義的properties不能使用插值
詳細設置
簡單值(simple value)
settings文件中,頂層元素中的一半以上都是簡單值。接下來讓我們看一看吧。
<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> <usePluginRegistry>false</usePluginRegistry> <offline>false</offline> ... </settings>
- localRepository:本地倉庫路徑,默認值為:
${user.home}/.m2/repository
。它允許所有的用戶從這個公共的本地倉庫構建系統。 - interactiveMode:默認為true,代表maven是否可以和用戶通過輸入進行交互。
- usePluginRegistry:默認為false,maven是否可以使用
${user.home}/.m2/plugin-registry.xml
管理插件版本。從2.0以后,我們是不需要使用這個屬性的,可以認為它廢棄了。 - offline:默認false,構建系統是否可以使用離線模式。在不能連接遠程倉庫的情況下,這個屬性是非常有用的。
插件組(Plugin Groups)
pluginGroups
包含了一組pluginGroup
元素,每一個都包含一個groupId
。當你在命令行使用插件,沒有提供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.mortbay.jetty</pluginGroup> </pluginGroups> ... </settings>
例如:我們執行org.mortbay.jetty:jetty-maven-plugin:run
時,可以使用短命令:mvn jetty:run
。
服務(Servers)
下載和部署的倉庫通常在pom.xml
中的repositories
和distributionManagement
元素中定義,但是像username
和password
時不應該在 單獨的pom文件中定義,這種配置信息應該在settings中定義。
<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:server的id,它和maven連接的repository或mirror的id匹配。
- username, password:用戶名和密碼,這兩個元素成對出現。
- privateKey, passphrase:私鑰文件和私鑰密碼,也是成對出現。
- filePermissions, directoryPermissions:當通過maven部署到遠程倉庫的時候,文件和目錄的權限通過這兩個元素指定。
當使用私鑰文件的時候,不要使用password
,要使用passphrase
。
鏡像(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>
- id, name:mirror的唯一標識和用戶設置的別名。當連接鏡像需要用戶名密碼或私鑰時,id要和
<servers>
中配置的id一致。 - url:鏡像的url。構建系統時將使用這個地址,而不是原始的倉庫地址。
- mirrorOf:倉庫鏡像的id。例如:指向maven的中央倉庫(https://repo.maven.apache.org/maven2/),設置為
center
。也可以使用一些高級的語法:repo1,repo2
或*,!inhouse
。
代理(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>
- id:proxy的唯一標識。
- active:代理是否有效。多個代理的情況下,只能有一個代理有效。
- protocol, host, port:代理的
protocol://host:port
,分隔成了多個元素。 - username, password:代理的用戶名和密碼,成對出現。
- nonProxyHosts:不使用代理的主機。使用逗號“,”分隔也可以。
鏡像和代理的區別:鏡像:改變原始的倉庫地址;代理:有些公司是不能上網的,他們需要配置代理才能訪問外網。
用戶配置(Profiles)
settings.xml
文件中的profile
是pom.xml
中的刪減版。它由activation
, repositories
, pluginRepositories
和 properties
組成。 而且只包含這4個元素,因為settings中的是全局配置,不是單個項目的配置。
如果settings中的profile是有效的,它將覆蓋掉pom中的相同id的profile。
激活(Activation)
它是profile中的一個元素,會在滿足activation
的條件時,激活狀態。
<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>
當activation
的條件滿足時,該profile將激活。
- jdk:
activation
有一個內嵌的,在jdk元素中已java為中心的檢查。當jdk的版本與配置的版本前綴匹配時,這個profile將被激活。 上面的例子中,jdk的版本1.5.0_06將匹配。范圍配置也是可以的,這里不做詳細介紹了。 - os:os可以定義一些運行系統的特殊屬性。由於比較少用,不做過多介紹,有興趣的可以查閱官方文檔。
- property:如果maven探測到一個屬性(這個屬性的值可以在pom.xml中配置),它的值與配置的值匹配,這個profile將被激活。上面的例子中, mavenVersion=2.0.3時,profile將激活。
- file:existence的文件存在,或者missing的文件不存在,條件將激活。
activation不是profile激活的唯一方式,settings.xml
文件中的activeProfile
元素包含了一個profile的id,可以同過命令行指定這個id來 激活profile。例如:-P test,將激活id為test的profile。
屬性(Properties)
maven的屬性是一個占位符,它可以在pom文件中,通過${X}進行訪問,X是屬性的名稱。它們有5中不同的形式:
env.X
:前綴是一個env,它將返回系統的環境變量。例如:${env.PATH}
將返回系統的環境變量$path。project.x
:訪問pom嗯我那件,點(.)在pom中代表層級的分隔。例如:<project><version>1.0</version></project>
可以通過${project.version}
訪問。settings.x
:同上,只是訪問的是settings文件。例如:<settings><offline>false</offline></settings>
可以通過${settings.offline}
訪問。- Java System Properties:java系統屬性,所有通過
java.lang.System.getProperties()
可以訪問到的屬性,在pom文件中都可以訪問。 例如:${java.home}
。 x
:<properties>
元素里配置的屬性。通過${someVal}
訪問。
<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>
上面的例子中,如果profile被激活,在pom中可以訪問${user.install}
。
倉庫(Repositories)
Repositories在這里不是本地倉庫的意思,而是遠程倉庫的集合。它在本地倉庫配置,maven通過它從遠程下載插件或者依賴。 不同的倉庫包含不同的項目,在激活的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:穩定版本或快照版本對應的配置。
- enabled:true或者false。對應版本的倉庫是否可用。
- updatePolicy:更新策略。它指定了多長時間更新一次,maven經常比較本地pom和遠程pom的時間戳。它的選項有:
always
、daily
(默認)、interval:X
(X是分鍾)、never
。 - checksumPolicy:當maven部署文件到倉庫時,它還會部署相對應的checksum文件。選項有:
ignore
,fail
, 或warn
,在checksum丟失或不正確的情況下執行。 - layout:在上面的配置中,它們都跟隨一個公共的布局。這在大多數情況下是正確的。Maven 2有一個倉庫的默認布局,但是maven 1.x有一個不同的布局。 使用這個元素可以選擇使用哪個版本的布局,
default
或legacy
。
插件倉庫(Plugin Repositories)
倉庫有兩種主要的類型。第一種是工件作為依賴,常說的jar包依賴。第二種是插件,maven的插件是一種特殊類型的工件,正因如此,maven把插件類型的倉庫 單獨提了出來。pluginRepositories
的元素和repositories
的元素非常的相似,它指定一個遠程插件倉庫的地址,可以在那里找到相應的maven插件。
激活profile(Active 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>
activeProfiles
元素包含了activeProfile
元素的集合,activeProfile
有一個profile的id值。在activeProfile
里定義的id都將被激活。 如果沒有找到匹配的profile,什么都不會生效。
好了,maven的settings.xml就為大家介紹的這里,有疑問可以隨時評論、留言。接下來還會介紹maven的pom.xml。
我的博客即將搬運同步至騰訊雲+社區,邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=1u17c7u12h747