Maven2的配置文件settings.xml(转)

http://maven.apache.org/settings.html
简介:

概览
当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们使用settings.xml中的settings元素来确定这些配置。这包含了本地仓库位置,远程仓库服务器以及认证信息等。
settings.xml存在于两个地方:
1.安装的地方:$M2_HOME/conf/settings.xml
2.用户的目录:${user.home}/.m2/settings.xml
前者又被叫做全局配置,后者被称为用户配置。如果两者都存在,它们的内容将被合并,并且用户范围的settings.xml优先。
如果你偶尔需要创建用户范围的settings,你可以简单的copy Maven安装路径下的settings到目录${user.home}/.m2。Maven默认的settings.xml是一个包含了注释和例子的模板,你可以快速的修改它来达到你的要求。

<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/>
    <interactiveMode/>
    <usePluginRegistry/>
    <offline/>
    <pluginGroups/>
    <servers/>
    <mirrors/>
    <proxies/>
    <profiles/>
    <activeProfiles/>
</settings>

settings的内容可以在下面这些地方篡改:
1.${user.home}和所有其他的系统属性
2.${env.HOME}等环境变量
注意:settins.xml中profiles下定义的属性不能被篡改。
配置细节:
简单的值
一半以上的*settings元素师简单的值,代表了一直处于活跃的构建系统的元素的取值范围。

<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>${user.home}/.m2/repository</localRepository>
    <interactiveMode>true</interactiveMode>
    <usePluginRegistry>false</usePluginRegistry>
    <offline>false</offline>
    ...
</settings>

localRepository:这个值是构建系统的本地仓库的路径。默认的值是${user.home}/.m2/repository.如果一个系统想让所有登陆的用户都用同一个本地仓库的话,这个值是极其有用的。

interactiveMode:如果Maven要试图与用户交互来得到输入就设置为true,否则就设置为false,默认为true。

usePluginRegistry:如果Maven使用${user.home}/.m2/plugin-registry.xml来管理plugin的版本,就设置为true,默认为false。

offline:如果构建系统要在离线模式下工作,设置为true,默认为false。如果构建服务器因为网络故障或者安全问题不能与远程仓库相连,那么这个设置是非常有用的。

插件组

这个元素包含了一系列pluginGroup元素,每个又包含了一个groupId。当一个plugin被使用,而它的groupId没有被提供的时候,这个列表将被搜索。这个列表自动的包含了org.apache.maven.plugins和org.codehaus.mojo。

<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">
    ...
    <pluginGroups>
        <pluginGroup>org.mortbay.jetty</pluginGroup>
    </pluginGroups>
    ...
</settings>

例如,有了上面的配置,Maven命令行可以使用简单的命令执行org.morbay.jetty:jetty-maven-plugin:run,如下

mvn jetty run

服务器

用来下载和部署的仓库是用POM中的repositories和distributionManagement元素来定义的。但是某些配置例如username和password就不应该随着pom.xml来分配了。这种类型的信息应该保存在构建服务器中的settings.xml中。

<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>server001</id>
    <username>my_login</username>
    <password>my_password</password>
    <privateKey>${user.home}/.ssh/id_dsa</privateKey>
    <passphrase>some_passphrase</passphrase>
    <filePermissions>664</filePermissions>
    <directoryPermissions>775</directoryPermissions>
    <configuration></configuration>
    </server>
    </servers>
    ...
</settings>

id:这是Server的ID(不是登录进来的user),与Maven想要连接上的repository/mirror中的id元素相匹配。
username,password:这两个元素成对出现,表示连接这个server需要验证username和password。
privateKey,passphrase:与前两个元素一样,这两个成对出现,分别指向了一个私钥(默认的是${user.home}/.ssh/id_dsa)和一个passphrase。passphrase和password元素可能在将来被客观化,但是现在必须以文本形式在settings.xml中设置。
filePermissions,directoryPermissions:当一个仓库文件或者目录在部署阶段被创建的时候,就必须用到权限许可。他们合法的值是三个数字,就像*nix中的文件权限,例如:664,775.
注意:如果你使用了一个私钥来登录server,那么password元素必须被省略,否则私钥将被忽视。
密码加密
一个新特征:服务器password和passphrase加密已经被升到2.1.0+

镜像

<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">
    ...
    <mirrors>
        <mirror>
            <id>planetmirror.com</id>
            <name>PlanetMirror Australia</name>
            <url>http://downloads.planetmirror.com/pub/maven2</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
    ...
</settings>

id,name:唯一的镜像标识和用户友好的镜像名称。id被用来区分mirror元素,并且当连接时候被用来获得相应的证书。
url:镜像基本的URL,构建系统敬将使用这个URL来连接仓库,而不是原来的仓库URL。
mirrorOf:镜像所包含的仓库的Id。例如,指向Maven central仓库的镜像(http://repo1.maven.org/maven2/),设置这个元素为central。更多的高级映射例如repo1,repo2 或者*,!inhouse都是可以的。没必要一定和mirror的id相匹配。

代理

<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">
    ...
    <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>
    ...
</settings>

id:proxy的唯一标识,用来区别proxy元素。

active:当proxy被激活的时候为true。当申明的代理很多的时候,这个很有用,但是同一时间仅有一个被激活。

protocol,host,port:代理地址protocol://host:port的分散形式。

username,password:两个元素成对出现,提供连接proxy服务器时的认证。

nonProxyHosts:这里列出了不需要使用代理的hosts。列表的分隔符是proxy服务器想要的类型。上面例子使用了pipe分隔符,逗号分隔符也比较通用。

配置文件

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来指定的。




http://www.cnblogs.com/yakov/archive/2011/11/26/maven2_settings.html
原文地址:http://maven.apache.org/settings.html



http://wenku.baidu.com/view/3fae1ce9102de2bd960588a5.html