在Maven中提供了一个settings.xml文件来定义Maven的全局环境信息。这个文件会存在于Maven的安装目录的conf子目录下面,或者是用户Home目录的.m2子目录下面。我们可以通过这个文件来定义本地仓库、远程仓库和联网使用的代理信息等。
安装好maven 之后,可以参考${maven.home}/conf/settings.xml这个文件,里面有很多例子并有详细解释。
其实相对于多用户的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的样子
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\\develop\\mavenRepository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
<pluginGroups>
<pluginGroup>com.your.plugins</pluginGroup>
</pluginGroups>
<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>
<servers>
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
</servers>
<mirrors>
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.5</id>
<activation>
<jdk>1.5</jdk>
</activation>
<repositories>
<repository>
<id>jdk15</id>
<name>jdk1.5</name>
<url>http://www.myhost.com/maven/jdk15</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>jdk-1.5</activeProfile>
</activeProfiles>
</settings>
localRepository:表示Maven用来在本地储存信息的本地仓库的目录。默认是用户Home目录下面的.m2/repository目录。
interactiveMode:表示是否使用交互模式,默认是true;如果设为false,那么当Maven需要用户进行输入的时候,它会使用一个默认值。
offline:表示是否离线,默认是false。这个属性表示在Maven进行项目编译和部署等操作时是否允许Maven进行联网来下载所需要的信息。
pluginGroups:在pluginGroups元素下面可以定义一系列的pluginGroup元素。pluginGroup元素指定的是plugin的groupId。
当一个plugin被使用,而它的groupId没有被提供的时候,这个列表将被搜索。
This is a list of additional group identifiers that will be searched when resolving plugins by their prefix。
当执行"mvn prefix:goal"这样的命令的时候,会通过这里指定的元素来通过前缀来解析plugin。
默认情况下,Maven会自动把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。
proxies:其下面可以定义一系列的proxy子元素,表示Maven在进行联网时需要使用到的代理。当设置了多个代理的时候第一个标记active为true的代理将会被使用。
servers:其下面可以定义一系列的server子元素,表示当需要连接到一个远程服务器的时候需要使用到的验证方式。这主要有username/password和privateKey/passphrase这两种方式。
This is a list of authentication profiles, keyed by the server-id used within the system.
mirrors:用于定义一系列的远程仓库的镜像。我们可以在pom中定义一个下载工件的时候所使用的远程仓库。但是有时候这个远程仓库会比较忙,所以这个时候人们就想着给它创建镜像以缓解远程仓库的压力,也就是说会把对远程仓库的请求转换到对其镜像地址的请求。每个远程仓库都会有一个id,这样我们就可以创建自己的mirror来关联到该仓库,那么以后需要从远程仓库下载工件的时候Maven就可以从我们定义好的mirror站点来下载,这可以很好的缓解我们远程仓库的压力。
不过,很多internal repository搭建工具往往也提供mirror服务,比如Nexus就可以让同一个URL,既用作internal repository,又使它成为所有repository的mirror。
- id:是用来区别mirror的,所有的mirror不能有相同的id
- mirrorOf:用来表示该mirror是关联的哪一个仓库,其值为其关联仓库的id。当要同时关联多个仓库时,这多个仓库之间可以用逗号隔开;当要关联所有的仓库时,可以使用“*”表示;当要关联除某一个仓库以外的其他所有仓库时,可以表示为“*,!repositoryId”;当要关联不是localhost或用file请求的仓库时,可以表示为“external:*”。
- url:表示该镜像的url。当Maven在建立系统的时候就会使用这个url来连接到我们的远程仓库。
mirrors可以配置多个mirror,每个mirror有id,name,url,mirrorOf属性,id是唯一标识一个mirror就不多说了,name貌似没多大用,相当于描述,url是官方的库地址,mirrorOf代表了一个镜像的替代位置,例如central就表示代替官方的中央库。
我本以为镜像库是一个分库的概念,就是说当a.jar在第一个mirror中不存在的时候,maven会去第二个mirror中查询下载。但事实却不是这样,当第一个mirror中不存在a.jar的时候,并不会去第二个mirror中查找,甚至于,maven根本不会去其他的mirror地址查询。
后来终于知道,maven的mirror是镜像,而不是“分库”,只有当前一个mirror无法连接的时候,才会去找后一个,类似于备份和容灾。
还有,mirror也不是按settings.xml中写的那样的顺序来查询的。
所谓的第一个并不一定是最上面的那个。
当有id为B,A,C的顺序的mirror在mirrors节点中,maven会根据字母排序来指定第一个,所以不管怎么排列,一定会找到A这个mirror来进行查找,当A无法连接,出现意外的情况下,才会去B查询。
profiles:用于指定一系列的profile。
profile元素由activation、repositories、pluginRepositories和properties四个元素组成。当一个profile在settings.xml中是处于活动状态并且在pom.xml中定义了一个相同id的profile时,settings.xml中的profile会覆盖pom.xml中的profile。
(1)activation:这是profile中最重要的元素。跟pom.xml中的profile一样,settings.xml中的profile也可以在特定环境下改变一些值,而这些环境是通过activation元素来指定的。
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.6</jdk>
<os>
<name>Windows 7</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>hello</name>
<value>world</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
...
</profile>
</profiles>
在上面这段代码中,当所有的约束条件都满足的时候就会激活这个profile。a)jdk:表示当jdk的版本满足条件的时候激活,在这里是1.6。这里的版本还可以用一个范围来表示,如
<jdk>[1.4,1.7)</jdk>表示1.4、1.5和1.6满足;
<jdk>[1.4,1.7]</jdk>表示1.4、1.5、1.6和1.7满足;
b)os:表示当操作系统满足条件的时候激活。
c)property:property是键值对的形式,表示当Maven检测到了这样一个键值对的时候就激活该profile。如果只包含name元素,表示如果存在这个属性,不管值多少,就激活这个属性。
这个时候如果要激活该profile的话,可以在调用Maven指令的时候加上参数hello并指定其值为world,如:mvn compile –Dhello=world
d)file:表示当文件存在或不存在的时候激活,exists表示存在,missing表示不存在。
2)properties:用于定义属性键值对的。当该profile是激活状态的时候,properties下面指定的属性都可以在pom.xml中使用。
An encouraged best practice for profile identification is to use a consistent naming convention for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
This will make it more intuitive to understand what the set of introduced profiles is attempting to accomplish, particularly when you only have a list of profile id's for debug.
另外,profiles中的定义也同样适用于pom.xml。例如我们在pom.xml中定义了如下的profile。
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<pom.dubbo.protocol.port>-1</pom.dubbo.protocol.port>
<pom.dubbo.registry.protocol>multicast</pom.dubbo.registry.protocol>
<pom.dubbo.registry.address>224.0.0.1:12345</pom.dubbo.registry.address>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<pom.dubbo.protocol.port>-1</pom.dubbo.protocol.port>
<pom.dubbo.registry.protocol>zookeeper</pom.dubbo.registry.protocol>
<pom.dubbo.registry.address>10.1.1.4:2181</pom.dubbo.registry.address>
</properties>
</profile>
</profiles>
然后在spring 的配置文件添加如下定义<dubbo:registry protocol="${pom.dubbo.registry.protocol}" address="${pom.dubbo.registry.address}" />
这样默认是dev的profile被激活,对应于开发版本。如果是生产版本,使用 mvn clean install -P prod 命令指定prod的profile。这样就可以很方便的切换开发和生产环境。
3)repositories:用于定义远程仓库的,当该profile是激活状态的时候,这里面定义的远程仓库将作为当前pom的远程仓库。
如果定义了多个仓库,maven下载文件的时候就会按照在配置文件中出现的顺序,在这些远程仓库中查找,直到找到为止。
如果所有的远程仓库中都没有找到,就会到中央仓库中查找。中央仓库的定义如下:
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
maven中的仓库分为两种,snapshot快照仓库和release发布仓库。snapshot快照仓库用于保存开发过程中的不稳定版本,release正式仓库则是用来保存稳定的发行版本。定义一个组件/模块为快照版本,只需要在pom文件中在该模块的版本号后加上-SNAPSHOT即可(注意这里必须是大写)。
下面是一个settings.xml的例子。
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>repo-iss</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers>
<mirrors>
<!-- osc镜像 -->
<mirror>
<!-- 镜像所有远程仓库,但不包括指定的仓库 -->
<id>mirror-osc</id>
<mirrorOf>external:*,!repo-osc-thirdparty,!repo-iss</mirrorOf>
<url>http://maven.oschina.net/content/groups/public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>profile-default</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>repo-osc-thirdparty</id>
<url>http://maven.oschina.net/content/repositories/thirdparty/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>profile-iss</id>
<repositories>
<repository>
<id>repo-iss</id>
<url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo-iss</id>
<url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>profile-default</activeProfile>
<!--<activeProfile>profile-iss</activeProfile>-->
</activeProfiles>
<!--
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>10.10.204.160</host>
<port>80</port>
</proxy>
</proxies>
-->
</settings>
原文链接:
1)http://haohaoxuexi.iteye.com/blog/1827778
2)http://my.oschina.net/qjx1208/blog/201085