#介绍# ##概述## settings.xml文件中的<settings />
包含一系列用于配置Maven执行方式的元素,如本地仓库位置、远程仓库服务器和身份验证信息等,相似pom.xml,但不捆绑到任何特定项目,或分发给受众。html
settings.xml文件通常位于如下两个位置:java
${maven.home}/conf/settings.xml
${user.home}/.m2/settings.xml
(没有改变settings.xml中本地仓库位置状况下此目录)前面位置的settings.xml文件为全局配置文件,后面的为用户配置文件。若是两个配置文件都存在,则以用户配置文件为主合并其内容。shell
提示:若是你须要新建立用户配置文件,最简单的方法是将Maven安装目录的全局settings.xml复制到${user.home}/.m2目录下。Maven的默认settings.xml是一个包含注释和示例的模板,所以你能够经过调整它以快速知足需求。express
如下是<settings />
下的顶级元素:apache
<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.xml中使用如下表达式来代替内容:安全
${user.home}
和全部其余系统属性(自Maven 3.0)${env.HOME}
等环境变量 请注意,settings.xml文件中<profiles>
元素中定义的properties不能用于${expression}。 #详细配置# ##简单值(单值)元素## 一半的顶级配置元素值在构建系统运行期间都是简单值。服务器
<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尝试与用户交互,需用户输入,不然设置为false
,默认为 true
。<usePluginRegistry />
:true
,Maven使用 ${user.home}/.m2/plugin-registry.xml
来管理插件版本, 不然false
。默认值为 false
。Note that for the current version of Maven 2.0, the plugin-registry.xml file should not be depended upon. Consider it dormant for now.(这句话暂时不知如何翻译出来稳当,毕竟已经到Maven 3.x时代)<offline />
:true
, 构建系统在离线模式运行,不然false
。默认值为false
。此元素对于因网络设置或安全缘由而没法链接到远程仓库的服务器很是有用。##<pluginGroups />##网络
此元素包含一组<pluginGroup />
元素,每一个<pluginGroup />
都包含一个groupId,当你使用插件而且在命令行中未提供groupId时,查询此列表。列表自动包含org.apache.maven.plugins
和org.codehaus.mojo
。ssh
<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>
例如,给定上述设置,在Maven命令行可以使用短命令mvn jetty:run
执行org.mortbay.jetty:jetty-maven-plugin:run
命令。maven
##<servers />##
POM文件中 <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>${user.home}/.ssh/id_dsa</privateKey> <passphrase>some_passphrase</passphrase> <filePermissions>664</filePermissions> <directoryPermissions>775</directoryPermissions> <configuration></configuration> </server> </servers> ... </settings>
${user.home}/.ssh/id_dsa
)和passphrase
。 passphrase
和 password
元素未来可能外化,但目前它们必须以纯文本形式配置在settings.xml文件中 。请注意:若是使用私钥登陆服务器,请确保没有配置<password />
元素值,不然,密钥将被忽略。
##<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>
<mirror />
元素,并在链接到镜像时从<servers />
部分选择相应的验证信息。central repository(https://repo.maven.apache.org/maven2/)
的一个镜像,将此元素设置为 central
。更高级的映射,如 repo1,repo2
or *,!
也是能够的.,但不能与<mirror />
的id相同。想更深刻的了解镜像,请阅读Guide to Mirror 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> ...
<proxy />
元素。true
,此代理激活。 当咱们声明了一组代理,而某个时刻只须要某一代理激活时用到。protocol://host:port
。##<profiles />##
settings.xml中的<profile>
元素是pom.xml中<profile>
元素的截断版本。由<activation />
、<properties />
、<repositories />
、<pluginRepositories />
元素组成。<profile>
元素只包含这四个元素,由于它们做为整个构建系统的一部分,而不是单个项目对象模型的配置。 若是profile在settings.xml处于活动状态,则其值将覆盖在POM或profiles.xml文件中任id相等的profile配置。
###<activation />###
<activation />
是profile的关键,像POM的profiles同样,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>
profile在知足全部指定的条件时激活。尽管这些条件不必定一会儿都能知足。
<jdk />
元素中有一个内置的以Java为中心的检查,当检测到匹配的jdk运行环境,profile被激活,上面的例子中,将匹配 JDK1.5.0_06。<os />
元素能够定义如上所示的一些操做系统特定属性。<value />
元素值,则此profile 将被激活。 <activation />
元素并非激活profile的惟一方式,settings.xml文件中<activeProfile />元素能够指定将被激活的profile id,profile也能够经过在命令行,使用-P标记后跟逗号分隔的列表来显式激活(如,-P test)。
想要查看哪一个profile处于激活状态,使用maven-help-plugin
。
mvn help:active-profiles
###<properties />###
Maven中的<properties />
是值占位符,相似于Ant中的<property />
,经过${X}
在POM文件中的任何地方能够访问它们的值(X为属性)。
它们有五种不一样的形式,均可以在settings.xml文件中使用:
${env.PATH}
包含 \$path
环境变量(Windows中为%PATH%
).${project.version}
访问<project><version>1.0</version></project>
值。${settings.offline}
访问<settings><offline>false</offline></settings>
值。java.lang.System.getProperties()
得到的属性都可用做POM属性。例如${java.home}
。<properties />
元素或外部文件中的属性,经过${someVar}
使用。 以下,若是此profile处于激活状态,可从POM中访问${user.install}
属性值。
<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>
###<repositories />###
远程仓库是远程项目集合,Maven使用这些项目来装填构建系统的本地仓库,Maven称之为插件和依赖。不一样的远程仓库可能包含不一样的项目,处于激活状态下的profile,会搜索匹配的release或snapshot版本构件。
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 />
,POM可以在单个仓库中独立于其余类型更改每一个类型的artifact的下载策略。例如,可能用于开发决定,仅启用快照下载。true
或false
,表示是否为相应类型启用此仓库。always
、daily
(默认)、interval:X
(X是一分钟为单位的整数)和never
。<checksumPolicy />
配置校验缺失或者不正确时的处理方式,有ignore
、fail
、warn
三个选项值。default
仍是legacy
。###<pluginRepositories />###
仓库存储两种主要类型的构件,一种是用做其余构件依赖项的组件,中央仓库的大多数构件属于这种类型。 另外一种构件类型是插件, Maven插件是一种特殊类型的构件,所以插件仓库可能独立于其余仓库。 <pluginRepositories />
元素的结构相似于<repositories />
元素, 每一个<pluginRepository />
元素指定了Maven能够查找到新插件的远程地址。
<a id="activeProfiles"></a>
##<activeProfiles />##
<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 />
元素,每一个<activeProfile />
元素都包含一个profile id。任何在<activeProfile />
元素中设置的profile id所对应的profile将处于激活状态,无论环境设置如何。 若是没有找到匹配的profile,将不产生任何效果。例如,<activeProfile />
设置profile id为env-test
,则pom.xml或profile.xml文件中设置id为env-test
的profile将激活,若是没有匹配的profile,则继续正常执行。
原网址连接Settings Reference