Maven全局配置文件settings.xml详解
目录
一、概要
1、settings.xml的作用
2、settings.xml文件位置
3、配置的优先级
二、settings.xml元素详解
1、顶级元素概览
1.1、LocalRepository
1.2、InteractiveMode
1.3、UsePluginRegistry
1.4、Offline
1.5、PluginGroups
1.6、Servers
1.7、Mirrors
1.8、Proxies
1.9、Profiles
1.10、ActiveProfiles
一、概要
1、settings.xml的作用
它是用来设置Maven参数的配置文件。并且,settings.xml是Maven的全局配置文件。settings.xml中包含类似本地仓库、远程仓库和联网使用的代理信息等配置。
2、settings.xml文件位置
settings.xml文件一般存在于Maven的安装目录的conf子目录下面,或者是用户目录的.m2子目录下面。
3、配置的优先级
其实相对于多用户的PC机而言,在Maven安装目录的conf子目录下面的settings.xml才是真正的全局的配置。而用户目录的.m2子目录下面的settings.xml的配置只是针对当前用户的。当这两个文件同时存在的时候,那么对于相同的配置信息用户目录下面的settings.xml中定义的会覆盖Maven安装目录下面的settings.xml中的定义。用户目录下的settings.xml文件一般是不存在的,但是Maven允许我们在这里定义我们自己的settings.xml,如果需要在这里定义我们自己的settings.xml的时候就可以把Maven安装目录下面的settings.xml文件拷贝到用户目录的.m2目录下,然后改成自己想要的样子。
二、settings.xml元素详解
1、顶级元素概览
<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>
1.1、LocalRepository
作用:该值表示构建系统本地仓库的路径。
其默认值:~/.m2/repository。
<localRepository>${user.home}/.m2/repositorylocalRepository>
1.2、InteractiveMode
作用:表示maven是否需要和用户交互以获得输入。
如果maven需要和用户交互以获得输入,则设置成true,反之则应为false。默认为true。
<interactiveMode>trueinteractiveMode>
1.3、UsePluginRegistry
作用:maven是否需要使用plugin-registry.xml文件来管理插件版本。
如果需要让maven使用文件~/.m2/plugin-registry.xml来管理插件版本,则设为true。默认为false。
<usePluginRegistry>falseusePluginRegistry>
1.4、Offline
作用:这个属性表示在Maven进行项目编译和部署等操作时是否允许Maven进行联网来下载所需要的信息。
如果构建系统需要在离线模式下运行,则为true,默认为false。
当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。
<offline>falseoffline>
1.5、PluginGroups
作用:在pluginGroups元素下面可以定义一系列的pluginGroup元素。表示当通过plugin的前缀来解析plugin的时候到哪里寻找。pluginGroup元素指定的是plugin的groupId。默认情况下,Maven会自动把org.apache.maven.plugins 和 org.codehaus.mojo 添加到pluginGroups下。
<pluginGroups> <pluginGroup>org.codehaus.mojopluginGroup> pluginGroups>
1.6、Servers
作用:一般,仓库的下载和部署是在pom.xml文件中的repositories 和 distributionManagement 元素中定义的。然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在pom.xml文件中配置,这些信息可以配置在 settings.xml 中。
<servers> <server> <id>server001id> <username>my_loginusername> <password>my_passwordpassword> <privateKey>${usr.home}/.ssh/id_dsaprivateKey> <passphrase>some_passphrasepassphrase> <filePermissions>664filePermissions> <directoryPermissions>775directoryPermissions> server> servers>
1.7、Mirrors
作用:用于定义一系列的远程仓库的镜像。我们可以在pom中定义一个下载工件的时候所使用的远程仓库。但是有时候这个远程仓库会比较忙,所以这个时候人们就想着给它创建镜像以缓解远程仓库的压力,也就是说会把对远程仓库的请求转换到对其镜像地址的请求。每个远程仓库都会有一个id,这样我们就可以创建自己的mirror来关联到该仓库,那么以后需要从远程仓库下载工件的时候Maven就可以从我们定义好的mirror站点来下载,这可以很好的缓解我们远程仓库的压力。在我们定义的mirror中每个远程仓库都只能有一个mirror与它关联,也就是说你不能同时配置多个mirror的mirrorOf指向同一个repositoryId。
<mirrors> <mirror> <id>mirrorIdid> <name>PlanetMirror Australianame> <url>http://downloads.planetmirror.com/pub/maven2url> <mirrorOf>repositoryIdmirrorOf> mirror> mirrors>
1.8、Proxies
作用:用来配置不同的代理。
<proxies> <proxy> <id>myproxyid> <active>trueactive> <protocol>httpprotocol> <host>proxy.somewhere.comhost> <port>8080port> <username>proxyuserusername> <password>somepasswordpassword> <nonProxyHosts>*.google.com|ibiblio.orgnonProxyHosts> proxy> proxies>
1.9、Profiles
作用:根据环境参数来调整构建配置的列表。
settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。它包含了id、activation、repositories、pluginRepositories和 properties元素。这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile。当所有的约束条件都满足的时候就会激活这个profile。
<profiles> <profile> <id>testid> <activation> <activeByDefault>falseactiveByDefault> <jdk>1.6jdk> <os> <name>Windows 7name> <family>Windowsfamily> <arch>x86arch> <version>5.1.2600version> os> <property> <name>mavenVersionname> <value>2.0.3value> property> <file> <exists>${basedir}/file2.propertiesexists> <missing>${basedir}/file1.propertiesmissing> file> activation> <properties /> <repositories /> <pluginRepositories /> ... profile> profiles>
1.9.1、Activation
作用:自动触发profile的条件逻辑。这是profile中最重要的元素。跟pom.xml中的profile一样,settings.xml中的profile也可以在特定环境下改变一些值,而这些环境是通过activation元素来指定的。activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id。profile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。
jdk:表示当jdk的版本满足条件的时候激活,在这里是1.6。这里的版本还可以用一个范围来表示,如
os:表示当操作系统满足条件的时候激活。
property:property是键值对的形式,表示当Maven检测到了这样一个键值对的时候就激活该profile。
(1)下面的示例表示当存在属性hello的时候激活该profile。
<property> <name>helloname> property>
(2)下面的示例表示当属性hello的值为world的时候激活该profile。
<property> <name>helloname> <value>worldvalue> property>
这个时候如果要激活该profile的话,可以在调用Maven指令的时候加上参数hello并指定其值为world,如:
mvn compile –Dhello=world
file:表示当文件存在或不存在的时候激活,exists表示存在,missing表示不存在。如下面例子表示当文件hello/world不存在的时候激活该profile。
<profile> <activation> <file> <missing>hello/worldmissing> file> activation> profile>
activeByDefault:当其值为true的时候表示如果没有其他的profile处于激活状态的时候,该profile将自动被激活。
properties:用于定义属性键值对的。当该profile是激活状态的时候,properties下面指定的属性都可以在pom.xml中使用。对应profile的扩展属性列表。
maven属性和ant中的属性一样,可以用来存放一些值。这些值可以在pom.xml中的任何地方使用标记${X}来使用,这里X是指属性的名称。属性有五种不同的形式,并且都能在settings.xml文件中访问。
<properties> <user.install>${user.home}/our-projectuser.install> properties>
注:如果该profile被激活,则可以在pom.xml中使用${user.install} 。
repositories:用于定义远程仓库的,当该profile是激活状态的时候,这里面定义的远程仓库将作为当前pom的远程仓库。它是maven用来填充构建系统本地仓库所使用的一组远程仓库。
<repositories> <repository> <id>codehausSnapshotsid> <name>Codehaus Snapshotsname> <releases> <enabled>falseenabled> <updatePolicy>alwaysupdatePolicy> <checksumPolicy>warnchecksumPolicy> releases> <snapshots> <enabled /> <updatePolicy /> <checksumPolicy /> snapshots> <url>http://snapshots.maven.codehaus.org/maven2url> <layout>defaultlayout> repository> repositories>
(1) releases、snapshots:这是对于工件的类型的限制。
(2) enabled:表示这个仓库是否允许这种类型的工件
(3) updatePolicy:表示多久尝试更新一次。可选值有always、daily、interval:minutes(表示每多久更新一次)和never。
(4) checksumPolicy:当Maven在部署项目到仓库的时候会连同校验文件一起提交,checksumPolicy表示当这个校验文件缺失或不正确的时候该如何处理,可选项有ignore、fail和warn。
pluginRepositories:在Maven中有两种类型的仓库,一种是存储工件的仓库,另一种就是存储plugin插件的仓库。pluginRepositories的定义和repositories的定义类似,它表示Maven在哪些地方可以找到所需要的插件。和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>
示例:
<activation> <activeByDefault>falseactiveByDefault> <jdk>1.5jdk> <os> <name>Windows XPname> <family>Windowsfamily> <arch>x86arch> <version>5.1.2600version> os> <property> <name>mavenVersionname> <value>2.0.3value> property> <file> <exists>${basedir}/file2.propertiesexists> <missing>${basedir}/file1.propertiesmissing> file> activation>
1.10、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-testactiveProfile> activeProfiles> ... settings>
------------------------------------------------------------------------------
原文链接: