[Forward] Chinese translation of the official documentation of the maven configuration file

Introduction:

Overview

When various configurations during the Maven run, such as pom.xml, do not want to be tied to a fixed project or assigned to users, we use the settings element in settings.xml to determine these configurations. This includes the location of the local repository, the remote repository server, and authentication information.

settings.xml exists in two places:

1. The installation place: $M2_HOME/conf/settings.xml

2. The user's directory: ${user.home}/.m2/settings.xml The

former is also called the global configuration, the latter These are called user profiles. If both are present, their contents will be merged and the user-scoped settings.xml takes precedence.

If you occasionally need to create user-wide settings, you can simply copy the settings from the Maven installation path to the directory ${user.home}/.m2. Maven's default settings.xml is a template with comments and examples that you can quickly modify to suit your requirements.

Here is an overview of the top-level 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 content of settings can be tampered with in the following places:

1. ${user.home} and all other system properties

2. Environment variables such as ${env.HOME}

Note : The properties defined under profiles in settins.xml cannot be tampered with.

Configuration Details:

Simple Values ​​More than

half of the top-level settings elements are simple values, representing a range of values ​​for elements that are always active in the build system.
<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 to the build system's local repository. The default value is ${user.home}/.m2/repository. This value is extremely useful if a system wants all logged in users to use the same local repository.

interactiveMode: Set to true if Maven is trying to interact with the user to get input, false otherwise, defaults to true.

usePluginRegistry: Set to true if Maven uses ${user.home}/.m2/plugin-registry.xml to manage the version of the plugin, defaults to false.

offline: Set to true if the build system is to work in offline mode, default to false. This setting is useful if the build server cannot connect to the remote repository due to network failures or security issues.

pluginGroups: This element contains a series of pluginGroup elements, each containing a groupId. This list will be searched when a plugin is used and its groupId is not provided. This list automatically includes org.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, with the above configuration, the Maven command line can execute org.morbay.jetty:jetty-maven-plugin:run with a simple command like
mvn jetty run

servers: The repositories used to download and deploy are repositories in the POM and distributionManagement element to define. But some configuration like username and password should not be assigned with pom.xml. This type of information should be kept in settings.xml in the build server.

<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 the logged in user), which matches the id element in the repository/mirror that Maven wants to connect to.

username, password: These two elements appear in pairs, indicating that connecting to this server requires authentication of username and password.

privateKey, passphrase: Like the first two elements, these two appear in pairs, pointing to a private key (default is ${user.home}/.ssh/id_dsa) and a passphrase. The passphrase and password elements may be objectified in the future, but must now be set in settings.xml as text.

filePermissions, directoryPermissions: Permissions must be used when a repository file or directory is created during the deployment phase. Their legal values ​​are three numbers, like file permissions in *nix, for example: 664, 775.

Note: If you use a private key to log into the server, the password element must be omitted, otherwise the private key will be ignored .

Password encryption

A new feature: server password and passphrase encryption has been upgraded to 2.1.0+

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: Unique image ID and user-friendly image name. The id is used to distinguish mirror elements, and is used to obtain the corresponding certificate when connecting.

url: The base URL of the mirror, which the build system will use to connect to the repository instead of the original repository URL.

mirrorOf: Id of the repository contained in the mirror. For example, to point to a mirror of the Maven central repository (http://repo1.maven.org/maven2/), set this element to central. More advanced mappings such as repo1, repo2 or *, !inhouse are possible. It doesn't have to match the mirror's id.

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 of the proxy, used to distinguish the proxy element.

active: true when the proxy is activated. This is useful when there are many proxies declared, but only one is active at a time.

protocol, host, port: Decentralized form of proxy address protocol://host:port.

username, password: The two elements appear in pairs, providing authentication when connecting to the proxy server.

nonProxyHosts: Hosts that do not need to use a proxy are listed here. The separator for the list is the type expected by the proxy server. The above example uses the pipe separator, and the comma separator is also more common.

The profile in the configuration file

settings.xml is a concise form of the profile in pom.xml. It contains activation (activation), repository (repositories), plugin repository (pluginRepositories) and properties (properties) elements. The profile element contains only these four elements because they refer to the entire build system, not individual POM configurations.

If a profile in settings is activated, its value will override the POM or any profiles with an equivalent ID in profiles.xml.

Activation

activations are the key to profiles, just like profiles in POM, the power of a profile is that it can modify some values ​​under certain circumstances. And these situations are specified by activation.
<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>


If all the specified conditions are met, the activation is triggered, and it does not need to be met all at once.

jdk: In the jdk element, activation has a built-in, java version detection. If the jdk version is detected as expected, then activate. In the above example, 1.5.0_06 is satisfied.

os: The os element can define some of the operating system specific attributes shown above.

property: If Maven detects the property of the corresponding name-value pair, then this profile will be activated.

file: If the given file exists, or does not exist then this profile will be activated.

Activation is not the only way to activate a profile. The activeProfile in settings.xml contains the id of the profile. They can also be activated explicitly via the command line, eg -P test.

If you want to see which profiles are activated during a build. Just use maven-help-plugin

mvn help:active-profiles

properties (properites)

Maven properties are placeholders for values, just like properties in Ant. If X is a property, then its value can be accessed anywhere in the POM using ${X}. They come from five different styles, all accessible from the settings.xml file.

1.env.X: Using the "env." prefix will return the current environment variables. For example, ${env.PATH} uses the $path environment variable.

2.project.X: A path separated by a dot ".", which is the value of the related element in the POM. For example: <project><version>1.0</version></project> can be accessed via ${project.version}.

3.settings.X: A path separated by a dot ".", which is the value of the corresponding element in settings.xml, for example: <settings><offline>false</offline></settings> can pass ${ settings.offline} to access.

4.Java system properties: All properties accessed through java.lang.System.getProperties() can be accessed like properties in POM, for example: ${java.home}

5.X: by <properties/> or external File-defined properties, the value can be accessed like this ${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>


If this profile is activated, then the property ${user.install} can be accessed.

Repositories A repository

is a collection of remote projects that Maven uses to build the local repository of the build system. It comes from a local repository called plugins and dependencies by Maven. Different remote repositories contain different projects, and when profiles are activated, they will need to find matching release or snapshot artifacts.

<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: This is the strategy for various components, releases or snapshots. Thanks to these two sets, a POM can change the current policy in a single repository without depending on the policy of the other. For example: one might only download snapshots for development.

enable: true or false, to mark whether the repository is activated for the respective type (release or snapshot).

updatePolicy: This element specifies the frequency of updates. Maven will compare the local POM with the remote timestamp. The optional items are: always, daily, interval:X,nerver.

checksumPolicy: When Maven deploys a file to the repository, it also deploys the corresponding checksum file. Optional: ignore, fail, warn, or an incorrect checksum.

layout: When describing repositories above, we mentioned that they have a uniform layout. This is absolutely correct. Use this to indicate whether it is default or legacy.

Plugin repositories (plugin repositories)

repositories contain two important types of artifacts. The first is used to make other components depend on components, which are most plugins in the central repository. Another type of component is a plugin. Maven plugins are themselves a special kind of artifact. Therefore, the plugin repository is separated from other repositories. Anyway, the structure of the pluginRepositories element module is very similar to the repositories module. The pluginRepository element points to a remote address where new plugins can be found.

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 puzzle in settings.xml is the activeProfiles element. It contains a series of activeProfile elements, each with a profile id value, and any profile with a profile id defined to activeProfile will be activated, regardless of other environment settings. If no matching profile is found, then do nothing. For example: if env-test is an activeProfile, a profile with the corresponding id in pom.xml or profile.xml will be activated. If no such profile is found, nothing is done and business as usual.


Original address: http://www.cnblogs.com/yakov/archive/2011/11/26/maven2_settings.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041964&siteId=291194637