Maven2 configuration file Settings.xml parsing

Source: Internet
Author: User
Tags xmlns ssh file permissions maven central

Brief introduction:

Overview

When the various configurations in Maven run, such as Pom.xml, do not want to bind to a fixed project or are to be assigned to a user, we use the settings element in Settings.xml to determine these configurations. This includes the local warehouse location, the remote warehouse server, and authentication information.

Settings.xml exists in two places:

1. Place of installation: $M 2_home/conf/settings.xml

2. User's Directory: ${user.home}/.m2/settings.xml

The former is also called the global configuration, which is called the user Configuration. If both are present, their contents are merged and the user-scoped settings.xml takes precedence.

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

Below 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 contents of settings can be tampered with in the following places:

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

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

Note: Properties defined under profiles in Settins.xml cannot be tampered with.
Configuration details:

A simple value

More than half of the top settings elements are simple values that represent the value range of elements that have been active in the building 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 local repository of the build system. The default value is ${user.home}/.m2/repository. This value is extremely useful if a system wants to make all logged-in users use the same local repository.

Interactivemode: Set to True if Maven tries to interact with the user to get input, otherwise it is set to false, which defaults to true.

Usepluginregistry: If Maven uses ${user.home}/.m2/plugin-registry.xml to manage the version of plugin, it is set to True and the default is False.

Offline: Set to True if the build system is to work in offline mode, false by default. This setting is useful if the build server is not connected to the remote repository because of a network failure or security issue.

Plug-in groups

This element contains a series of plugingroup elements, each of which contains a groupid. When a plugin is used and its groupid is not provided, this list will be searched. This list automatically contains the Org.apache.maven.plugins and Org.codehaus.mojo.

1 <settings xmlns= "http://maven.apache.org/SETTINGS/1.0.0"
 2           xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
 3           xsi:schemalocation=" http://maven.apache.org/SETTINGS/1.0.0
 4/             http Maven.apache.org/xsd/settings-1.0.0.xsd ">
 5 ...
 6     <pluginGroups>
 7         <pluginGroup>org.mortbay.jetty</pluginGroup>
 8     </pluginGroups>
 9 ...
Ten </settings>

For example, with the configuration above, the MAVEN command line can execute org.morbay.jetty:jetty-maven-plugin:run with simple commands, as follows

MVN Jetty Run

Server

The repositories used to download and deploy are defined using the repositories and distributionmanagement elements in the POM. However, some configurations, such as username and password, should not be assigned with Pom.xml. This type of information should be stored in the settings.xml of the build server.

1 <settings xmlns= "http://maven.apache.org/SETTINGS/1.0.0"
 2           xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
 3           xsi:schemalocation=" http://maven.apache.org/SETTINGS/1.0.0
 4/             http Maven.apache.org/xsd/settings-1.0.0.xsd ">
 5 ...
 6     <servers>
 7     <server>
 8     <id>server001</id>
 9     < 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 user logged in) that 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 this server requires validating username and password.

Privatekey,passphrase: As with the first two elements, these two pairs appear, pointing to a private key (the default is ${USER.HOME}/.SSH/ID_DSA) and a passphrase. Passphrase and password elements may be objective in the future, but must now be set in text form in Settings.xml.

Filepermissions,directorypermissions: When a repository file or directory is created in the deployment phase, permission permission must be used. Their legal value is three numbers, just like the file permissions in *nix, for example: 664,775.

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

Password encryption A new feature: server password and passphrase encryption has been promoted to 2.1.0+ mirror

1 <settings xmlns= "http://maven.apache.org/SETTINGS/1.0.0"
 2           xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
 3           xsi:schemalocation=" http://maven.apache.org/SETTINGS/1.0.0
 4/             http Maven.apache.org/xsd/settings-1.0.0.xsd ">
 5 ...
 6     <mirrors>
 7         <mirror>
 8             <id>planetmirror.com</id>
 9             <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 differentiate the mirror element and is used to obtain the corresponding certificate when connected.

URL: Mirroring the basic URL, the build system will use this URL to connect to the repository instead of the original warehouse URL.

MIRROROF: The ID of the warehouse that the image contains. For example, a mirror that points to the MAVEN central repository (http://repo1.maven.org/maven2/), setting this element to Central. More advanced mappings, such as REPO1,REPO2 or *,!inhouse, are all possible. There is no need to match the mirror ID.

Agent

1 <settings xmlns= "http://maven.apache.org/SETTINGS/1.0.0"
 2           xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
 3           xsi:schemalocation=" http://maven.apache.org/SETTINGS/1.0.0
 4/             http Maven.apache.org/xsd/settings-1.0.0.xsd ">
 5 ...
 6     <proxies>
 7         <proxy>
 8             <id>myproxy</id>
 9             < active>true</active>             <protocol>http</protocol>             

The unique identifier of the id:proxy, used to distinguish the proxy element.

Active: True when proxy is activated. This is useful when the stated agent is many, but only one at a time is activated.

Protocol,host,port: The decentralized form of proxy address protocol://host:port.

Username,password: Two elements in pairs appear, provide authentication when connecting proxy server.

Nonproxyhosts: Hosts that do not need to use proxies are listed here. The delimiter for the list is the type that the proxy server wants. The above example uses the pipe delimiter, and the comma delimiter is more general.

Configuration file

The profile in Settings.xml is a concise form of the profile in Pom.xml. It contains the activation (activation), warehouse (repositories), plug-in warehouse (pluginrepositories), and attribute (properties) elements. The profile element contains only these four elements because they relate to the entire build system, not to individual pom configurations.

If the profile in Settings is activated, its value will overload the profiles of any equality ID in the POM or profiles.xml.

Activation (activation)

Activations is the key to profile, just as the ability of profiles,profile in Pom is that it can modify some values in certain situations. These conditions are specified by activation.

 1 <settings xmlns= "http://maven.apache.org/SETTINGS/1.0.0" 2 xmlns:xsi= "http://www.w3.org/2001/XMLSchema-in Stance "3 xsi:schemalocation=" http://maven.apache.org/SETTINGS/1.0.0 4 http://maven.apache.org/xsd
 /settings-1.0.0.xsd "> 5 ... 6 <profiles> 7 <profile> 8 <id>test</id> 9 &LT;ACTIVATION&G
T                 <activeByDefault>false</activeByDefault> <jdk>1.5</jdk> 12 <os> <name>windows xp</name> <family> Windows</family> <arch>x86</arch> <version>5.1.2600& lt;/version> </os> <property> <name>mave Nversion</name> <value>2.0.3</value> </propErty> <file> <exists>${basedir}/file2.properties</exists> 2             4 <missing>${basedir}/file1.properties</missing> </file> 26
</activation> 27 ...
</profile> </profiles> 30 ... </settings>

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

JDK: In the JDK element, activation has a built-in, Java version detection. If the JDK version is detected as expected, it is activated. In the above example, the 1.5.0_06 is satisfied.

The Os:os element can define some of the operating system-specific properties shown above.

Property: If Maven detects the attribute of the corresponding name-value pair, the profile will be activated.

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

Activation is not the only way to activate profile. The Activeprofile in Settings.xml contains the ID of the profile. They can also be explicitly activated via the command line, such as-P test.

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

MVN help:active-profiles

Properties (properites)

The Maven property is a value placeholder, just like an attribute 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 of which can be accessed from the settings.xml file.

1.env. X: Use "env." The prefix will return the current environment variable. such as Env. Path is using {env. Path} is using the PATH environment variable.

2.project. X: one point "." The split path, in the Pom, is the value of the associated element. For example: 1.0 can be accessed via ${project.version}.

3.settings. X: one point "." The partitioned path, in Settings.xml, is the value of the corresponding element, for example: false can be accessed through ${settings.offline}.

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

5.X: Properties defined by or external files, values can be accessed in this way ${somevar}

1 <settings xmlns= "http://maven.apache.org/SETTINGS/1.0.0"
 2           xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
 3           xsi:schemalocation=" http://maven.apache.org/SETTINGS/1.0.0
 4/             http Maven.apache.org/xsd/settings-1.0.0.xsd ">
 5 ...
 6     <profiles>
 7         <profile>
 8
 ... 9             <properties>                 <user.install>${user.home}/our-project</user.install>             </properties>             ...         </profile>     </profiles>
... </settings>

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

Warehouse (repositories)

A repository is a collection of remote projects that MAVEN uses to build a local repository for building systems. It comes from a local repository that Maven is called a plugin and relies on. Different remote repositories contain different items, and when profile is activated, they will need to find a matching release or snapshot component.

1 <settings xmlns= "http://maven.apache.org/SETTINGS/1.0.0" 2 xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-ins Tance "3 xsi:schemalocation=" http://maven.apache.org/SETTINGS/1.0.0 4 http://maven.apache.org/xsd/
 Settings-1.0.0.xsd "> 5 ...
 6 <profiles> 7 <profile> 8 ... 
9 <repositories> <repository> <id>codehausSnapshots</id>                     <name>codehaus snapshots</name> <releases> 14                     <enabled>false</enabled> <updatePolicy>always</updatePolicy> 16 <checksumPolicy>warn</checksumPolicy> </releases> &LT;SN Apshots> <enabled>true</enabled> <updatepolicy>never&lt ;/updatepolicy> &LT;checksumpolicy>fail</checksumpolicy> </snapshots> <url>http:/ /snapshots.maven.codehaus.org/maven2</url> <layout>default</layout> &L
t;/repository> </repositories> <pluginRepositories> 28 ...
</pluginRepositories> 30 ...
</profile> </profiles> 33 ... </settings>

Releases,snapshots: This is a strategy for various components, release or snapshot. Because of these two sets, the POM can change the current policy in a single warehouse without relying on another policy. For example: A person may only download snapshot for development.

Enable:true or FALSE to flag whether the warehouse is active for its own type (release or snapshot).

Updatepolicy: This element indicates the frequency of the update. Maven compares the local pom with the remote timestamp. The optional items are: Always,daily,interval:x,nerver.

Checksumpolicy: When Maven deploys files to the warehouse, it also deploys the appropriate checksum files. Optional: Ignore,fail,warn, or incorrect checksum.

Layout: In the above description of the warehouse, we mentioned that they have a unified layout. It's all right. Use this to indicate whether it is default or legacy.

Plugin Warehouse (plugin repositories)

The warehouse contains two important types of artifacts. The first is the component that is used for other component dependencies, which is the majority of the plugins in the central repository. Another type of component is a plug-in. The Maven plugin itself is a special component. As a result, the plugin repository is separated from the other warehouses. In any case, the structure of the Pluginrepositories element module is very similar to the repositories module. The pluginrepository element points to a remote address where a new plug-in can be found.

Activation configuration (Active Profiles)

1 <settings xmlns= "http://maven.apache.org/SETTINGS/1.0.0"
2           xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
3           xsi:schemalocation=" http://maven.apache.org/SETTINGS/1.0.0
4/             http Maven.apache.org/xsd/settings-1.0.0.xsd ">
5 ...
6     <activeProfiles>
7         <activeProfile>env-test</activeProfile>
8     </ Activeprofiles>
9 </settings>

Settings.xml the last puzzle is the activeprofiles element. It contains a series of activeprofile elements, each with a value for the profile ID, and any profile ID defined to the activeprofile will be activated, regardless of other environment settings. If no matching profile is found, then nothing is done. For example: If Env-test is a activeprofile, a profile with the corresponding ID in Pom.xml or profile.xml will be activated. If no such profile is found, nothing will be done, as usual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.