简介
settings.xml文件中的settings元素包含了很多子元素,它们定义的值被用来配置Maven的执行情况。该settings文件的设置会被应用到很多个项目上,因此这里的设置不应该和任何一个特定的项目绑定,并且该设置的内容也不应该分发给它人。该文件定义的值包括本地仓库地址,候选的远程仓库仓库服务器,以及一些认证信息。settings.xml文件可位于两个地方:
• Maven安装目录: $M2_HOME/conf/settings.xml
• 用户特定的Settings文件: ~/.m2/settings.xml
这里是settings元素下最顶层元素的概览:
settings.xml中顶层元素的概览
- <settings
xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
- http://maven.apache.org/xsd/settings-1.0.0.xsd">
- <localRepository/>
- <interactiveMode/>
- <usePluginRegistry/>
- <offline/>
- <pluginGroups/>
- <servers/>
- <mirrors/>
- <proxies/>
- <profiles/>
- <activeProfiles/>
- </settings>
简单介绍一下几个主要的配置因素:
localRepository:表示本地库的保存位置,也就是maven2主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。
offline:如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。
Servers
- <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> -
<configuration></configuration> -
</server> -
</servers>
id:server 的id,用于匹配distributionManagement库id,比较重要。
username, password:用于登陆此服务器的用户名和密码
privateKey, passphrase:设置private key,以及passphrase
filePermissions, directoryPermissions:当库文件或者目录创建后,需要使用权限进行访问。参照unix文件许可,如664和775
Mirrors
表示镜像库,指定库的镜像,用于增加其他库
- <mirrors>
-
<mirror> -
<id>planetmirror.com</id> -
<name>PlanetMirror Australia</name> -
<url>http://downloads.planetmirror.com/pub/maven2</url> -
<mirrorOf>central</mirrorOf> -
</mirror> -
</mirrors>
id,name:唯一的标志,用于区别镜像
url:镜像的url
mirrorOf:此镜像指向的服务id
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:代理的标志
active:是否激活代理
protocol, host, port:protocol://host:port 代理
username, password:用户名和密码
nonProxyHosts: 不需要代理的host
Profiles
- <repositories>
-
<repository> -
<id>repo-local</id> -
<name>Internal 开发库</name> -
<url>http://192.168.0.2:8082/repo-local</url> -
<releases> -
<enabled>true</enabled> -
<updatePolicy>never</updatePolicy> -
<checksumPolicy>warn</checksumPolicy> -
</releases> -
<snapshots> -
<enabled>false</enabled> -
</snapshots> -
<layout>default</layout> -
</repository> -
</repositories> -
<pluginRepositories> -
<pluginRepository> -
<id>repo-local</id> -
<name>Internal 开发库</name> -
<url>http://192.168.0.2:8082/repo-local</url> -
<releases> -
<enabled>true</enabled> -
<updatePolicy>never</updatePolicy> -
<checksumPolicy>warn</checksumPolicy> -
</releases> -
<snapshots> -
<enabled>false</enabled> -
</snapshots> -
<layout>default</layout> -
</pluginRepository> -
</pluginRepositories>
releases, snapshots:每个产品的版本的Release或者snapshot(注:release和snapshot的区别,release一般是比较稳定的版本,而snapshot基本上不稳定,只是作为快照)
properties
包括以下的5种类型值:
- env.X,返回当前的环境变量
- project.x:返回pom中定义的元素值,如project.version
- settings.x:返回settings.xml中定义的元素
- java 系统属性:所有经过java.lang.System.getProperties()返回的值
- x:用户自己设定的值
Activation
- <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>
jdk:如果匹配指定的jdk版本,将会激活
os:操作系统
property:如果maven能检测到相应的属性
file: 用于判断文件是否存在或者不存在
除了使用activation来激活profile,同样可以通过activeProfiles来激活
Active Profiles
表示激活的profile,通过profile id来指定。
- <activeProfiles>
-
<activeProfile>env-test</activeProfile> 指定的profile id -
</activeProfiles>