1. 程式人生 > >Maven項目settings.xml的配置

Maven項目settings.xml的配置

inter 種類 inf declare 需要 where resolv 屬性 config

原文地址 http://www.cnblogs.com/DreamDrive/p/5571916.html

在Maven中提供了一個settings.xml文件來定義Maven的全局環境信息。這個文件會存在於Maven的安裝目錄的conf子目錄下面,或者是用戶家目錄的.m2子目錄下面。我們可以通過這個文件來定義本地倉庫、遠程倉庫和聯網使用的代理信息等。

其實相對於多用戶的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>
 
  </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>
  </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>
技術分享

如上是原作者的,如下是我本機的:

技術分享
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user, 
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they‘re all using the same Maven
 |                 installation). It‘s normally provided in 
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<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
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
 <localRepository>F:\Development\m2\repository</localRepository>
  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <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
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the ‘id‘ attribute below).
     | 
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are 
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->
    
    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   | 
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred 
   | server for that repository.
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of 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
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is 
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a 
   | value of ‘1.4‘ might activate a profile when the build is executed on a JDK version of ‘1.4.2_07‘.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | 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.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property ‘target-env‘ with a value of ‘dev‘,
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |   
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set ‘target-env‘ to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>
技術分享

我本機是使用的默認配置,可見默認配置很多都註釋掉了,只有一個<localRepository>

settings.xml中主要包括以下元素:

localRepository:表示Maven用來在本地儲存信息的本地倉庫的目錄。默認是用戶家目錄下面的.m2/repository目錄。

interactiveMode:表示是否使用交互模式,默認是true;如果設為false,那麽當Maven需要用戶進行輸入的時候,它會使用一個默認值。

offline:表示是否離線,默認是false。這個屬性表示在Maven進行項目編譯和部署等操作時是否允許Maven進行聯網來下載所需要的信息。

pluginGroups:在pluginGroups元素下面可以定義一系列的pluginGroup元素。表示當通過plugin的前綴來解析plugin的時候到哪裏尋找。pluginGroup元素指定的是plugin的groupId。默認情況下,Maven會自動把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。

proxies:其下面可以定義一系列的proxy子元素,表示Maven在進行聯網時需要使用到的代理。當設置了多個代理的時候第一個標記active為true的代理將會被使用。下面是一個使用代理的例子:

技術分享
<proxies>
  <proxy>
      <id>xxx</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>用戶名</username>
      <password>密碼</password>
      <host>代理服務器地址</host>
      <port>代理服務器的端口</port>
      <nonProxyHosts>不使用代理的主機</nonProxyHosts>
  </proxy>
</proxies>
技術分享

servers:其下面可以定義一系列的server子元素,表示當需要連接到一個遠程服務器的時候需要使用到的驗證方式。這主要有username/password和privateKey/passphrase這兩種方式。以下是一個使用servers的示例:

技術分享
  <servers>
    <server>
      <id>id</id>
      <username>用戶名</username>
      <password>密碼</password>
    </server>
  </servers>
技術分享

mirrors:用於定義一系列的遠程倉庫的鏡像。我們可以在pom中定義一個下載工件的時候所使用的遠程倉庫。但是有時候這個遠程倉庫會比較忙,所以這個時候人們就想著給它創建鏡像以緩解遠程倉庫的壓力,也就是說會把對遠程倉庫的請求轉換到對其鏡像地址的請求。每個遠程倉庫都會有一個id,這樣我們就可以創建自己的mirror來關聯到該倉庫,那麽以後需要從遠程倉庫下載工件的時候Maven就可以從我們定義好的mirror站點來下載,這可以很好的緩解我們遠程倉庫的壓力。在我們定義的mirror中每個遠程倉庫都只能有一個mirror與它關聯,也就是說你不能同時配置多個mirror的mirrorOf指向同一個repositoryId。

看以下是一個使用mirrors的例子:

技術分享
<mirrors>
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>定義一個容易看懂的名稱 </name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
</mirrors>
技術分享

id:是用來區別mirror的,所有的mirror不能有相同的id

mirrorOf:用來表示該mirror是關聯的哪一個倉庫,其值為其關聯倉庫的id。當要同時關聯多個倉庫時,這多個倉庫之間可以用逗號隔開;當要關聯所有的倉庫時,可以使用“*”表示;當要關聯除某一個倉庫以外的其他所有倉庫時,可以表示為“*,!repositoryId”;當要關聯不是localhost或用file請求的倉庫時,可以表示為“external:*”。

url:表示該鏡像的url。當Maven在建立系統的時候就會使用這個url來連接到我們的遠程倉庫。

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>mavenVersion</name>
          <value>2.0.3</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
      ...
    </profile>
  </profiles>
技術分享

在上面這段代碼中,當所有的約束條件都滿足的時候就會激活這個profile。

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滿足;

os:表示當操作系統滿足條件的時候激活。

property:property是鍵值對的形式,表示當Maven檢測到了這樣一個鍵值對的時候就激活該profile。

(1)下面的示例表示當存在屬性hello的時候激活該profile。

<property>
       <name>hello</name>
</property>

(2)下面的示例表示當屬性hello的值為world的時候激活該profile。

<property>
       <name>hello</name>
       <value>world</value>
</property>

這個時候如果要激活該profile的話,可以在調用Maven指令的時候加上參數hello並指定其值為world,如:

mvn compile –Dhello=world

file:表示當文件存在或不存在的時候激活,exists表示存在,missing表示不存在。如下面的例子表示當文件hello/world不存在的時候激活該profile。

技術分享
<profile>
       <activation>
              <file>
                     <missing>hello/world</missing>
              </file>
       </activation>
</profile>
技術分享

activeByDefault:當其值為true的時候表示如果沒有其他的profile處於激活狀態的時候,該profile將自動被激活。

(2)properties:用於定義屬性鍵值對的。當該profile是激活狀態的時候,properties下面指定的屬性都可以在pom.xml中使用。

(3)repositories:用於定義遠程倉庫的,當該profile是激活狀態的時候,這裏面定義的遠程倉庫將作為當前pom的遠程倉庫。

技術分享
      <repositories>
        <repository>
          <id>codehausSnapshots</id>
          <name>Codehaus Snapshots</name>
          <releases>
            <enabled>false</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>fail</checksumPolicy>
          </snapshots>
          <url>http://snapshots.maven.codehaus.org/maven2</url>
          <layout>default</layout>
        </repository>
      </repositories>
技術分享

releases、snapshots:這是對於工件的類型的限制。

enabled:表示這個倉庫是否允許這種類型的工件

updatePolicy:表示多久嘗試更新一次。可選值有always、daily、interval:minutes(表示每多久更新一次)和never。

checksumPolicy:當Maven在部署項目到倉庫的時候會連同校驗文件一起提交,checksumPolicy表示當這個校驗文件缺失或不正確的時候該如何處理,可選項有ignore、fail和warn。

(4)pluginRepositories:在Maven中有兩種類型的倉庫,一種是存儲工件的倉庫,另一種就是存儲plugin插件的倉庫。pluginRepositories的定義和repositories的定義類似,它表示Maven在哪些地方可以找到所需要的插件。

activeProfiles底包含一系列的activeProfile元素,表示對於所有的pom都處於活躍狀態的profile。如:

  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>

Maven項目settings.xml的配置