- settings.xml
settings.xml
文件中的settings元素包含了很多子元素,它们定义的值被用来配置Maven的执行情况。该settings文件的设置会被应用到很多个项目上,因此这里的设置不应该和任何一个特定的项目绑定,并且该设置的内容也不应该分发给它人。该文件定义的值包括本地仓库地址,候选的远程仓库仓库服务器,以及一些认证信息。settings.xml
文件可位于两个地方:
1. Maven安装目录:
$M2_HOME/conf/settings.xml(如:D:\java\apache-maven-2.2.1\conf\settings.xml)
2. 用户特定的settings文件:
~/.m2/settings.xml(如:XP下Administrator用户的settings.xml文件的路径就是C:\Documents and Settings\Administrator\.m2\settings.xml)
特定用户的settings.xml文件的配置可以覆盖Maven安装目录下settings.xml文件的配置,windows用户设置Maven安装目录下的配置文件就好了,免得重装系统后再重新设置配置文件。
- 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"> <!-- 构建系统本地仓库的路径。其默认值为~/.m2/repository --> <localRepository/> <!-- 如果Maven需要和用户交互以获得输入,则设置成true,反之则应为false。默认为true。 --> <interactiveMode/> <!-- 如果需要让Maven使用文件值为~/.m2/plugin-registry.xml来管理插件版本,则设为true。默认为false。 --> <usePluginRegistry/> <!-- 如果构建系统需要在离线模式下运行,则为true,默认为false。当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。 --> <offline/> <!-- 该元素包含一个pluginGroup元素列表,每个子元素包含了一个groupId。当我们使用某个插件,并且没有在命令行为其提供groupId的时候,Maven就会使用该列表。默认情况下该列表包含了org.apache.maven.plugins。 --> <pluginGroups/> <!-- 服务器。POM中的distributionManagement元素定义了部署的仓库。然而,一些设置如安全证书不应该和pom.xml一起分发。这种类型的信息应该存在于构建服务器上的settings.xml文件中 --> <servers> <server> <!-- 这是server的id(注意不是用户登陆的id),该id与distributionManagement中repository元素的id相匹配 --> <id>server001</id> <!-- 服务器认证所需要的登录名 --> <username>my_login</username> <!-- 服务器认证所需要的密码 --> <password>my_password</password> <!-- 私钥的路径(默认是/home/j2ee-hudson/.ssh/id_dsa) --> <privateKey>${usr.home}/.ssh/id_dsa</privateKey> <!-- 密语 --> <passphrase>some_passphrase</passphrase> <!-- 仓库文件权限。合法的值是一个三位数字,其对应了*nix文件系统的权限,如664,或者775。 --> <filePermissions>664</filePermissions> <!-- 目录权限。合法的值是一个三位数字,其对应了*nix文件系统的权限,如664,或者775。 --> <directoryPermissions>775</directoryPermissions> <!-- 格式:DOM。Extra configuration for the transport layer --> <configuration></configuration> </server> </servers> <!-- 镜像 --> <mirrors> <mirror> <!-- 镜像的id。id用来区分不同的mirror元素。 --> <id>planetmirror.com</id> <!-- 镜像的名称 --> <name>PlanetMirror Australia</name> <!-- 该镜像的URL。构建系统会优先考虑使用该URL,而非使用默认的服务器URL。 --> <url>http://downloads.planetmirror.com/pub/maven2</url> <!-- 被镜像的服务器的id。例如,如果我们要设置了一个Maven中央仓库(http://repo1.maven.org/maven2)的镜像,就需要将该元素设置成central。这必须和中央仓库的id “central”完全一致。 --> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <!-- 代理 --> <proxies> <proxy> <!-- 该代理的唯一定义符,用来区分不同的proxy元素。 --> <id>myproxy</id> <!-- true则激活代理。当我们声明了一组代理,而某个时候只需要激活其中某一个或几个代理的时候,该元素就可以派上用处。 --> <active>true</active> <!-- 该代理的协议。protocol://host:port,(协议://主机名:端口) --> <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> <!-- settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。它包含了activation, repositories, pluginRepositories 和 properties元素。这里的profile元素只包含这四个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。 如果一个settings中的profile被激活,它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile。 --> <profiles> <profile> <id>test</id> <!-- activation是profile的开启钥匙。如POM中的profile一样,profile的力量来自于它能够在某些特定的环境中自动使用某些特定的值;这些环境通过activation元素指定。当所有指定的条件符合的时候,profile机会被激活,虽然大部分情况我们不会需要所有的条件。 --> <activation> <activeByDefault>false</activeByDefault> <!-- activation通过其jdk元素,提供了一个内置的,Java-核心的检查器。如果我们运行的一个jdk版本号,这里所配置的值能作为前缀与之匹配,激活就会发生。在上述的例子中,1.5.0_06就能够匹配。 --> <jdk>1.5</jdk> <!-- os元素可以定义一些操作系统相关的属性。 --> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> <!-- 如果Maven检测到某一个属性(其值可以在POM中通过${名称}引用),其拥有对应的名称和值,profile就会被激活。 --> <property> <name>mavenVersion</name> <value>2.0.3</value> </property> <!-- 通过提供一个文件名,通过检测该文件的存在或不存在来激活profile。 --> <file> <exists> /data/hudson-temporal-data/hudson-orchestrator-home/workspace/Book-To-Production/content-zh/file2.properties</exists> <missing>/data/hudson-temporal-data/hudson-orchestrator-home/workspace/Book-To-Production/content-zh/file1.properties</missing> </file> </activation> </profile> <profile> ... <!-- 属性。Maven属性和Ant中的属性一样,可以用来存放一些值。这些值可以在POM中的任何地方使用标记${X}来使用,这里X是指属性的名称。属性有五种不同的形式,并且都能在settings.xml文件中访问。如果该profile激活,属性${user.install}就可以在POM中被访问。 --> <properties> <user.install>/workspace/pjojectA/our-project</user.install> </properties> ... </profile> <profile> ... <!-- 仓库。仓库是Maven用来填充构建系统本地仓库所使用的一组远程项目。而Maven是从本地仓库中使用其插件和依赖。不同的远程仓库可能含有不同的项目,而在某个激活的profile下,可能定义了一些仓库来搜索需要的发布版或快照版构件。有了Nexus,这些应该交由Nexus完成 --> <repositories> <repository> <id>codehausSnapshots</id> <name>Codehaus Snapshots</name> <!-- 发布版策略配置 --> <releases> <!-- true或者false表示该仓库是否为某种类型构件(发布版或者快照版)开启。 --> <enabled>false</enabled> <!-- 该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。这里的选项是:always(一直),daily(默认,每日),interval:X(这里X是以分钟为单位的时间间隔),或者never(从不)。 --> <updatePolicy>always</updatePolicy> <!-- 当Maven将构件部署到仓库中时,它也会部署对应的校验和文件。当没有校验和文件,或者该文件不正确时,你的选项有ignore(忽略),fail(失败),或者warn(警告)。 --> <checksumPolicy>warn</checksumPolicy> </releases> <!-- 快照版策略配置 --> <snapshots> <enabled>true</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> <url>http://snapshots.maven.codehaus.org/maven2</url> <!-- 在上面的仓库描述中,它们都遵循一个共同的布局。大部分情况都是这样。Maven 2为其仓库提供了一个默认的布局;然而,Maven 1.x有一种不同的布局。我们可以使用该元素指定布局是default(默认)还是legacy(遗留)。 --> <layout>default</layout> </repository> </repositories> <!-- 插件仓库。仓库是两种主要构件的家。第一种构件被用作其它构件的依赖。这是中央仓库中存储大部分构件类型。另外一种构件类型是插件。Maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories元素的结构和repositories元素的结构类似。每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。 --> <pluginRepositories> ... </pluginRepositories> ... </profile> </profiles> <!-- 激活的Profile。activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id,任何在activeProfile中定义的profile id,不论环境设置如何,其对应的profile都会被激活。如果没有匹配的profile,则什么都不会发生。。profile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。要了解在某个特定的构建中哪些profile会激活,可以使用maven-help-plugin(mvn help:active-profiles)。 --> <activeProfiles> <activeProfile>env-test</activeProfile> </activeProfiles> </settings>
- demo
<settings> <!-- 构建系统本地仓库的路径。其默认值为~/.m2/repository --> <localRepository>D:/java/apache-maven-2.2.1/local/repository</localRepository> <!-- 该元素包含一个pluginGroup元素列表,每个子元素包含了一个groupId。当我们使用某个插件,并且没有在命令行为其提供groupId的时候,Maven就会使用该列表。默认情况下该列表包含了org.apache.maven.plugins。 --> <pluginGroups> <pluginGroup>org.codehaus.mojo</pluginGroup> </pluginGroups> <mirrors> <!--This sends everything else to /public --> <mirror> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://192.168.1.1:8080/nexus/content/groups/public</url> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <!--Enable snapshots for the built in central repo to direct --> <!--all requests to nexus via the mirror --> <repositories> <repository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <!--make the profile active all the time --> <activeProfile>nexus</activeProfile> </activeProfiles> </settings>