maven 配置篇 之 settings.xml

来源:互联网 发布:金10数据手机软件 编辑:程序博客网 时间:2024/06/08 11:15

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

 

Settings Reference

  1. Introduction
    1. Quick Overview
  2. Settings Details
    1. Simple Values
    2. Plugin Groups
    3. Servers
      1. Password Encryption
    4. Mirrors
    5. Proxies
    6. Profiles
      1. Activation
      2. Repositories
      3. Plugin Repositories
    7. Active Profiles

Introduction

Quick Overview

The settings element in the settings.xml file contains elements used to define values which configure Maven execution in various ways, like thepom.xml, but should not be bundled to any specific project, or distributed to an audience. These include values such as the local repository location, alternate remote repository servers, and authentication information.

There are two locations where a settings.xml file may live:

  • The Maven install: $M2_HOME/conf/settings.xml
  • A user's install: ${user.home}/.m2/settings.xml

The former settings.xml are also called global settings, the latter settings.xml are referred to as user settings. If both files exists, their contents gets merged, with the user-specificsettings.xml being dominant.

Tip: If you need to create user-specific settings from scratch, it's easiest to copy the global settings from your Maven installation to your${user.home}/.m2 directory. Maven's default settings.xml is a template with comments and examples so you can quickly tweak it to match your needs.

Here is an overview of the top elements under 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>

The contents of the settings.xml can be interpolated using the following expressions:

  1. ${user.home} and all other system properties (since Maven 3.0)
  2. ${env.HOME} etc. for environment variables

Note that properties defined in profiles within the settings.xml cannot be used for interpolation.

Settings Details

Simple Values

Half of the top-level settings elements are simple values, representing a range of values which describe elements of the build system that are active full-time.

<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: This value is the path of this build system's local repository. The default value is${user.home}/.m2/repository. This element is especially useful for a main build server allowing all logged-in users to build from a common local repository.
  • interactiveMode: true if Maven should attempt to interact with the user for input,false if not. Defaults to true.
  • usePluginRegistry: true if Maven should use the ${user.home}/.m2/plugin-registry.xml file to manage plugin versions, defaults tofalse. Note that for the current version of Maven 2.0, the plugin-registry.xml file should not be depended upon. Consider it dormant for now.
  • offline: true if this build system should operate in offline mode, defaults tofalse. This element is useful for build servers which cannot connect to a remote repository, either because of network setup or security reasons.

Plugin Groups

This element contains a list of pluginGroup elements, each contains a groupId. The list is searched when a plugin is used and the groupId is not provided in the command line. This list automatically containsorg.apache.maven.plugins and 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>

For example, given the above settings the Maven command line may execute org.mortbay.jetty:jetty-maven-plugin:run with the truncated command:

mvn jetty:run

Servers

The repositories for download and deployment are defined by the repositories and distributionManagement elements of the POM. However, certain settings such asusername and password should not be distributed along with the pom.xml. This type of information should exist on the build server in the 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: This is the ID of the server (not of the user to login as) that matches theid element of the repository/mirror that Maven tries to connect to.
  • username, password: These elements appear as a pair denoting the login and password required to authenticate to this server.
  • privateKey, passphrase: Like the previous two elements, this pair specifies a path to a private key (default is${user.home}/.ssh/id_dsa) and a passphrase, if required. The passphrase and password elements may be externalized in the future, but for now they must be set plain-text in thesettings.xml file.
  • filePermissions, directoryPermissions: When a repository file or directory is created on deployment, these are the permissions to use. The legal values of each is a three digit number corrosponding to *nix file permissions, ie. 664, or 775.

Note: If you use a private key to login to the server, make sure you omit the<password> element. Otherwise, the key will be ignored.

Password Encryption

A new feature - server password and passphrase encryption has been added to 2.1.0+. See detailson this page

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: The unique identifier and user-friendly name of this mirror. Theid is used to differentiate between mirror elements and to pick the corresponding credentials from the<servers> section when connecting to the mirror.
  • url: The base URL of this mirror. The build system will use this URL to connect to a repository rather than the original repository URL.
  • mirrorOf: The id of the repository that this is a mirror of. For example, to point to a mirror of the Mavencentral repository (http://repo.maven.apache.org/maven2/), set this element tocentral. More advanced mappings like repo1,repo2 or *,!inhouse are also possible. This must not match the mirrorid.

For a more in-depth introduction of mirrors, please read the Guide to Mirror Settings.

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: The unique identifier for this proxy. This is used to differentiate betweenproxy elements.
  • active: true if this proxy is active. This is useful for declaring a set of proxies, but only one may be active at a time.
  • protocol, host, port: The protocol://host:port of the proxy, seperated into discrete elements.
  • username, password: These elements appear as a pair denoting the login and password required to authenticate to this proxy server.
  • nonProxyHosts: This is a list of hosts which should not be proxied. The delimiter of the list is the expected type of the proxy server; the example above is pipe delimited - comma delimited is also common.

Profiles

The profile element in the settings.xml is a truncated version of thepom.xml profile element. It consists of the activation,repositories, pluginRepositories and properties elements. Theprofile elements only include these four elements because they concerns themselves with the build system as a whole (which is the role of thesettings.xml file), not about individual project object model settings.

If a profile is active from settings, its values will override any equivalently ID'd profiles in a POM orprofiles.xml file.

Activation

Activations are the key of a profile. Like the POM's profiles, the power of a profile comes from its ability to modify some values only under certain circumstances; those circumstances are specified via anactivation element.

<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 occurs when all specified criteria have been met, though not all are required at once.

  • jdk: activation has a built in, Java-centric check in thejdk element. This will activate if the test is run under a jdk version number that matches the prefix given. In the above example,1.5.0_06 will match. Ranges are also supported as of Maven 2.1. See the maven-enforcer-plugin for more details about supported ranges.
  • os: The os element can define some operating system specific properties shown above. See themaven-enforcer-plugin for more details about OS values.
  • property: The profile will activate if Maven detects a property (a value which can be dereferenced within the POM by${name}) of the corresponding name=value pair.
  • file: Finally, a given filename may activate the profile by theexistence of a file, or if it is missing.

The activation element is not the only way that a profile may be activated. Thesettings.xml file's activeProfile element may contain the profile'sid. They may also be activated explicitly through the command line via a comma separated list after the-P flag (e.g. -P test).

To see which profile will activate in a certain build, use the maven-help-plugin.

mvn help:active-profiles

Properties

Maven properties are value placeholder, like properties in Ant. Their values are accessible anywhere within a POM by using the notation${X}, where X is the property. They come in five different styles, all accessible from thesettings.xml file:

  1. env.X: Prefixing a variable with "env." will return the shell's environment variable. For example,${env.PATH} contains the $path environment variable (%PATH% in Windows).
  2. project.x: A dot (.) notated path in the POM will contain the corresponding element's value. For example:<project><version>1.0</version></project> is accessible via ${project.version}.
  3. settings.x: A dot (.) notated path in the settings.xml will contain the corresponding element's value. For example:<settings><offline>false</offline></settings> is accessible via ${settings.offline}.
  4. Java System Properties: All properties accessible via java.lang.System.getProperties() are available as POM properties, such as${java.home}.
  5. x: Set within a <properties /> element or an external files, the value may be used as${someVar}.
<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>      ...      <properties>        <user.install>${user.home}/our-project</user.install>      </properties>      ...    </profile>  </profiles>  ...</settings>

The property ${user.install} is accessible from a POM if this profile is active.

Repositories

Repositories are remote collections of projects from which Maven uses to populate the local repository of the build system. It is from this local repository that Maven calls it plugins and dependencies. Different remote repositories may contain different projects, and under the active profile they may be searched for a matching release or snapshot artifact.

<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>      ...      <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>      <pluginRepositories>        ...      </pluginRepositories>      ...    </profile>  </profiles>  ...</settings>
  • releases, snapshots: These are the policies for each type of artifact, Release or snapshot. With these two sets, a POM has the power to alter the policies for each type independent of the other within a single repository. For example, one may decide to enable only snapshot downloads, possibly for development purposes.
  • enabled: true or false for whether this repository is enabled for the respective type (releases orsnapshots).
  • updatePolicy: This element specifies how often updates should attempt to occur. Maven will compare the local POM's timestamp (stored in a repository's maven-metadata file) to the remote. The choices are:always, daily (default), interval:X (where X is an integer in minutes) ornever.
  • checksumPolicy: When Maven deploys files to the repository, it also deploys corresponding checksum files. Your options are toignore, fail, or warn on missing or incorrect checksums.
  • layout: In the above description of repositories, it was mentioned that they all follow a common layout. This is mostly correct. Maven 2 has a default layout for its repositories; however, Maven 1.x had a different layout. Use this element to specify which if it is default or legacy.

Plugin Repositories

Repositories are home to two major types of artifacts. The first are artifacts that are used as dependencies of other artifacts. These are the majority of plugins that reside within central. The other type of artifact is plugins. Maven plugins are themselves a special type of artifact. Because of this, plugin repositories may be separated from other repositories (although, I have yet to hear a convincing argument for doing so). In any case, the structure of thepluginRepositories element block is similar to the repositories element. ThepluginRepository elements each specify a remote location of where Maven can find new plugins.

Active 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">  ...  <activeProfiles>    <activeProfile>env-test</activeProfile>  </activeProfiles></settings>

The final piece of the settings.xml puzzle is the activeProfiles element. This contains a set ofactiveProfile elements, which each have a value of a profile id. Any profile id defined as an activeProfile will be active, reguardless of any environment settings. If no matching profile is found nothing will happen. For example, ifenv-test is an activeProfile, a profile in a pom.xml (orprofile.xml with a corrosponding id will be active. If no such profile is found then execution will continue as normal.

 

<?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.0http://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>
  -->

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

 

http://zyl.iteye.com/blog/41612

maven2 比起maven1 来说,需要配置的文件少多了,主要集中在pom.xml和settings.xml中。
    先来说说settings.xml,settings.xml对于maven来说相当于全局性的配置,用于所有的项目。在maven2中存在两个 settings.xml,一个位于maven2的安装目录conf下面,作为全局性配置。对于团队设置,保持一致的定义是关键,所以 maven2/conf下面的settings.xml就作为团队共同的配置文件。保证所有的团队成员都拥有相同的配置。当然对于每个成员,都需要特殊的 自定义设置,如用户信息,所以另外一个settings.xml就作为本地配置。默认的位置为:${user.dir} /.m2/settings.xml目录中(${user.dir} 指windows 中的用户目录)。
    settings.xml基本结构如下:
   

xml 代码
 
  1. <settings xmlns="http://maven.apache.org/POM/4.0.0"  
  2.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  4.                                http://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   <localRepository/>  
  6.   <interactiveMode/>  
  7.   <usePluginRegistry/>  
  8.   <offline/>  
  9.   <pluginGroups/>  
  10.   <servers/>  
  11.   <mirrors/>  
  12.   <proxies/>  
  13.   <profiles/>  
  14.   <activeProfiles/>  
  15. </settings>  


简单介绍一下几个主要的配置因素:
localRepository:表示本地库的保存位置,也就是maven2主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。
offline:如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。
Servers
   在POM中的 distributionManagement元素定义了开发库。然而,特定的username和pwd不能使用于pom.xml,所以通过此配置来保存server信息
 

xml 代码
 
  1. <servers>  
  2.    <server>  
  3.      <id>server001</id>  
  4.      <username>my_login</username>  
  5.      <password>my_password</password>  
  6.      <privateKey>${usr.home}/.ssh/id_dsa</privateKey>  
  7.      <passphrase>some_passphrase</passphrase>  
  8.      <filePermissions>664</filePermissions>  
  9.      <directoryPermissions>775</directoryPermissions>  
  10.      <configuration></configuration>  
  11.    </server>  
  12.  </servers>   


 

  • id:server 的id,用于匹配distributionManagement库id,比较重要。
  • username, password:用于登陆此服务器的用户名和密码
  • privateKey, passphrase:设置private key,以及passphrase
  • filePermissions, directoryPermissions:当库文件或者目录创建后,需要使用权限进行访问。参照unix文件许可,如664和775

Mirrors
表示镜像库,指定库的镜像,用于增加其他库
 

xml 代码
 
  1. <mirrors>  
  2.    <mirror>  
  3.      <id>planetmirror.com</id>  
  4.      <name>PlanetMirror Australia</name>  
  5.      <url>http://downloads.planetmirror.com/pub/maven2</url>  
  6.      <mirrorOf>central</mirrorOf>  
  7.    </mirror>  
  8.  </mirrors>  


 

  • id,name:唯一的标志,用于区别镜像
  • url:镜像的url
  • mirrorOf:此镜像指向的服务id

Proxies
此设置,主要用于无法直接访问中心的库用户配置。
 

xml 代码
 
  1. <proxies>  
  2.    <proxy>  
  3.      <id>myproxy</id>  
  4.      <active>true</active>  
  5.      <protocol>http</protocol>  
  6.      <host>proxy.somewhere.com</host>  
  7.      <port>8080</port>  
  8.      <username>proxyuser</username>  
  9.      <password>somepassword</password>  
  10.      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>  
  11.    </proxy>  
  12.  </proxies>  


 

  • id:代理的标志
  • active:是否激活代理
  • protocol, host, port:protocol://host:port 代理
  • username, password:用户名和密码
  • nonProxyHosts: 不需要代理的host

Profiles
  类似于pom.xml中的profile元素,主要包括activation,repositories,pluginRepositories 和properties元素
  刚开始接触的时候,可能会比较迷惑,其实这是maven2中比较强大的功能。从字面上来说,就是个性配置。
  单独定义profile后,并不会生效,需要通过满足条件来激活。
 repositories 和pluginRepositories
 定义其他开发库和插件开发库。对于团队来说,肯定有自己的开发库。可以通过此配置来定义。
 如下的配置,定义了本地开发库,用于release 发布。
   

xml 代码
 
  1. <repositories>  
  2.         <repository>  
  3.           <id>repo-local</id>  
  4.        <name>Internal 开发库</name>  
  5.        <url>http://192.168.0.2:8082/repo-local</url>  
  6.           <releases>  
  7.             <enabled>true</enabled>  
  8.             <updatePolicy>never</updatePolicy>  
  9.             <checksumPolicy>warn</checksumPolicy>  
  10.           </releases>  
  11.           <snapshots>  
  12.             <enabled>false</enabled>  
  13.           </snapshots>  
  14.           <layout>default</layout>  
  15.         </repository>  
  16.       </repositories>  
  17.       <pluginRepositories>  
  18.     <pluginRepository>  
  19.     <id>repo-local</id>  
  20.     <name>Internal 开发库</name>  
  21.     <url>http://192.168.0.2:8082/repo-local</url>  
  22.     <releases>  
  23.             <enabled>true</enabled>  
  24.             <updatePolicy>never</updatePolicy>  
  25.             <checksumPolicy>warn</checksumPolicy>  
  26.     </releases>  
  27.     <snapshots>  
  28.     <enabled>false</enabled>  
  29.     </snapshots>  
  30.     <layout>default</layout>  
  31.     </pluginRepository>  
  32.     </pluginRepositories>  


releases, snapshots:每个产品的版本的Release或者snapshot(注:release和snapshot的区别,release一般是比较稳定的版本,而snapshot基本上不稳定,只是作为快照)

properties
  maven 的properties作为placeholder值,如ant的properties。
包括以下的5种类型值:

  1. env.X,返回当前的环境变量
  2. project.x:返回pom中定义的元素值,如project.version
  3. settings.x:返回settings.xml中定义的元素
  4. java 系统属性:所有经过java.lang.System.getProperties()返回的值
  5. x:用户自己设定的值

Activation
  用于激活此profile
 

xml 代码
 
  1. <activation>  
  2.         <activeByDefault>false</activeByDefault>  
  3.         <jdk>1.5</jdk>  
  4.         <os>  
  5.           <name>Windows XP</name>  
  6.           <family>Windows</family>  
  7.           <arch>x86</arch>  
  8.           <version>5.1.2600</version>  
  9.         </os>  
  10.         <property>  
  11.           <name>mavenVersion</name>  
  12.           <value>2.0.3</value>  
  13.         </property>  
  14.         <file>  
  15.           <exists>${basedir}/file2.properties</exists>  
  16.           <missing>${basedir}/file1.properties</missing>  
  17.         </file>  
  18.       </activation>  


 

  • jdk:如果匹配指定的jdk版本,将会激活
  • os:操作系统
  • property:如果maven能检测到相应的属性
  • file: 用于判断文件是否存在或者不存在


除了使用activation来激活profile,同样可以通过activeProfiles来激活
Active Profiles
表示激活的profile,通过profile id来指定。
 

xml 代码
 
  1. <activeProfiles>  
  2.     <activeProfile>env-test</activeProfile> 指定的profile id  
  3.   </activeProfiles>