配置文件
名称
setting.xml
位置
maven的配置文件有二处:
1.安装的地方:$M2_HOME/conf/settings.xml 该处的配置对全局有效,如果安装在全局的用户下,对所有的用户均有效
2. 用户的目录:${user.home}/.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>
配置文件中可以使用的环境变量:
${user.home} and all other system properties (since Maven 3.0)
${env.HOME} etc. for environment variables
localRepository
定义本地仓库的存储位置,默认为~/.m2/repository目录下,修改这个可以更改本地仓库存储位置。如
<localRepository>/home/work/maven/repository</localRepository>
interactiveMode
表示是否使用交互模式,默认是true;如果设为false,那么当Maven需要用户进行输入的时候,它会使用一个默认值。
offline
表示是否离线,默认是false。这个属性表示在Maven进行项目编译和部署等操作时是否允许Maven进行联网来下载所需要的信息。
pluginGroups
解释:
这个元素包含了一系列pluginGroup元素,每个又包含了一个groupId。当一个plugin被使用,而它的groupId没有被提供的时候,这个列表将被搜索。这个列表自动的包含了org.apache.maven.plugins和org.codehaus.mojo。
默认情况下,Maven会自动把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。自定义插件的时候,通常会用到这个
usePluginRegistry
如果Maven使用${user.home}/.m2/plugin-registry.xml来管理plugin的版本,就设置为true,默认为false。maven3已废弃
servers
其下面可以定义一系列的server子元素,表示当需要连接到一个远程服务器的时候需要使用到的验证方式。这主要有username/password和privateKey/passphrase这两种方式。以下是一个使用servers的示例:
<servers>
<server>
<id>id</id>
<username>用户名</username>
<password>密码</password>
</server>
</servers>
注意: server中id必须唯一,且该ID会与repository/proxy/mirror 中的ID一一对应
mirrors
用于定义一系列的远程仓库的镜像。我们可以在pom中定义一个下载工件的时候所使用的远程仓库。但是有时候这个远程仓库会比较忙,所以这个时候人们就想着给它创建镜像以缓解远程仓库的压力,也就是说会把对远程仓库的请求转换到对其镜像地址的请求。每个远程仓库都会有一个id,这样我们就可以创建自己的mirror来关联到该仓库,那么以后需要从远程仓库下载工件的时候Maven就可以从我们定义好的mirror站点来下载,这可以很好的缓解我们远程仓库的压力。在我们定义的mirror中每个远程仓库都只能有一个mirror与它关联,也就是说你不能同时配置多个mirror的mirrorOf指向同一个repositoryId。
mirrorOf为* 代表代理所有
mirrorOf external:*代表排除所有,多个匹配用逗号分隔,!排除单个
例如:
<mirrors>
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>定义一个容易看懂的名称 </name>
<url>http://my.repository.com/repo/path</url>
</mirror>
</mirrors>
proxies
用户下载代理
<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>
id:proxy的唯一标识,用来区别proxy元素。
active:当proxy被激活的时候为true。当申明的代理很多的时候,这个很有用,但是同一时间仅有一个被激活。
protocol,host,port:代理地址protocol://host:port的分散形式。
username,password:两个元素成对出现,提供连接proxy服务器时的认证。
nonProxyHosts:这里列出了不需要使用代理的hosts。列表的分隔符是proxy服务器想要的类型。上面例子使用了pipe分隔符,逗号分隔符也比较通用。
profiles
settings.xml中的profile是pom.xml中的profile的简洁形式。它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。
如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。
激活(activation)
activations是profile的关键,就像POM中的profiles,profile的能力在于它在特定情况下可以修改一些值。而这些情况是通过activation来指定的。
<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>
如果所有指定的条件都达到了,那么,activation就被触发,而且不需要一次性全部达到。
jdk:在jdk元素中,activation有一个内建的,java版本检测。如果检测到jdk版本与期待的一样,那么就激活。在上面的例子中,1.5.0_06是满足的。
os:os元素可以定义一些上面所示的操作系统特定的属性。
property:如果Maven检测到相应的名值对的属性,那么,这个profile将被激活。
file:如果给定的文件存在,或者不存在那么将激活这个profile。
activation并不是唯一激活profile的途径。settings.xml中的activeProfile包含了profile的id。他们也可以通过命令行来显式的激活,例如-P test。
如果你想查看在一个构建过程中有哪些profile会被激活。就使用maven-help-plugin
mvn help:active-profiles
activeProfiles
<activeProfiles>
<activeProfile>env-test</activeProfile>
</activeProfiles>