`

Maven实战(九)—— settings.xml配置文件

 
阅读更多

settings.xml配置文件详解:

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下的顶层元素的一个概览:

<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>

 

以下是对每个顶层元素的详解:

1. localRepository:本地仓库绝对路径。

 

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

 

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

 

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

 

5. pluginGroups插件组。

这个元素包含了一系列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

 

6. servers:服务器组。

例如:

<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元素必须被省略,否则私钥将被忽视。

 

7. mirrors:镜像组。

例如:

<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相匹配。

 

8. proxies:代理组。

例如:

<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分隔符,逗号分隔符也比较通用。

 

9.  profiles:配置文件。

settings.xml中的profile是pom.xml中的profile的简洁形式。它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。

例如:

<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">

    ...

    <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>

    ...

</settings>

其中:

如果所有指定的条件都达到了,那么,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

 

10. activeProfiles:激活profile。

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

settings.xml配置文件的配置过程方法:

1.配置本地仓库:

<localRepository>D:/apache-maven-3.0.4/repository</localRepository>。

 

2.设置代理:

  <proxy>

      <id>optional</id>

      <active>true</active>

      <protocol>http</protocol>

      <username>guwq</username>

      <password>guwq123</password>

      <host>172.16.9.12</host>

      <port>8088</port>

      <nonProxyHosts>cld72.rdserver.*|local.*|*.si-tech.com.cn|172.*|127.*</nonProxyHosts>

  </proxy>

 

3.设置访问私服的认证:

  <servers>

    <server>

      <id>Snapshots</id>

      <username>admin</username>

      <password>admin123</password>

    </server>

    <server>

      <id>Releases</id>

      <username>admin</username>

      <password>admin123</password>

    </server>

  </servers>

 

4.配置profile私服信息:

<profile>

<id>nexus</id>

<repositories>

       <repository>

         <id>nexus</id>

         <name>Nexus</name>

         <url>http://cld72.rdserver.com.cn:9090/nexus/content/groups/public/</url>

         <snapshots>

         <enabled>true</enabled>

         <updatePolicy>always</updatePolicy>  

         </snapshots>

         <releases>

         <enabled>true</enabled>

         <!--<updatePolicy>always</updatePolicy>-->

         </releases>

       </repository>

</repositories>

<pluginRepositories>

       <pluginRepository>

         <id>nexus</id>

         <name>Nexus</name>

         <url>http://cld72.rdserver.com.cn:9090/nexus/content/groups/public/</url>

         <snapshots><enabled>true</enabled></snapshots>

         <releases><enabled>true</enabled></releases>

       </pluginRepository>

</pluginRepositories>

</profile>

 

5.激活profile:

  <activeProfiles>

      <!-- 此处填写激活的profile_ID -->

      <!-- <activeProfile>nexus</activeProfile> -->

      <activeProfile>nexus</activeProfile>

  </activeProfiles>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics