Detailed explanation of maven's settings configuration file

Posted by heybret on Fri, 14 Jan 2022 14:53:10 +0100

catalogue

1, Overview

1,settings. The role of XML

2,settings.xml file location

3. Configured priority

2, Settings XML element details

1. Top level element overview

1.1,LocalRepository

1.2,InteractiveMode

1.3,UsePluginRegistry

1.4,Offline

1.5,PluginGroups

1.6,Servers

1.7,Mirrors

1.8,Proxies

1.9,Profiles

1.10,ActiveProfiles

1, Overview

1,settings. The role of XML

It is a configuration file used to set Maven parameters. And, settings XML is Maven's global configuration file. settings.xml contains configurations such as local warehouse, remote warehouse and agent information for networking.

2,settings.xml file location

settings.xml files generally exist in the conf subdirectory of Maven's installation directory or in the user directory m2 subdirectory.

3. Configured priority

In fact, compared with multi-user PC s, the settings under the conf subdirectory of Maven installation directory XML is the real global configuration. The user directory Settings under the m2 subdirectory The configuration of XML is only for the current user. When the two files exist at the same time, the settings under the user directory for the same configuration information The settings defined in the Maven installation directory will be overwritten Definition in XML. Settings in the user directory XML files generally don't exist, but Maven allows us to define our own settings here XML, if you need to define our own settings here XML, you can set the settings.xml under the Maven installation directory Copy the XML file to the user directory m2 directory, and then change it to what you want.

2, Settings XML element details

1. Top level element overview

<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
                          https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository/>
  <interactiveMode/>
  <usePluginRegistry/>
  <offline/>
  <pluginGroups/>
  <servers/>
  <mirrors/>
  <proxies/>
  <profiles/>
  <activeProfiles/>
</settings>

1.1,LocalRepository

Role: this value indicates the path to build the local warehouse of the system.
Its default value: ~ / m2/repository.

<localRepository>${user.home}/.m2/repository</localRepository>

1.2,InteractiveMode

Function: indicates whether maven needs to interact with the user to obtain input.
If maven needs to interact with the user to get input, it should be set to true, otherwise it should be false. The default is true.

<interactiveMode>true</interactiveMode>

1.3,UsePluginRegistry

Function: whether maven needs to use plugin registry XML file to manage the plug-in version.
If necessary, let maven use the file ~ / m2/plugin-registry.xml to manage the plug-in version, set to true. The default is false.

<usePluginRegistry>false</usePluginRegistry>

1.4,Offline

Function: this attribute indicates whether Maven is allowed to connect to the Internet to download the required information during Maven's project compilation and deployment.
If the build system needs to run in offline mode, it is true and the default is false.
This configuration is useful when the build server cannot connect to the remote warehouse due to network settings or security factors.

<offline>false</offline>

1.5,PluginGroups

Function: a series of pluginGroup elements can be defined under the pluginGroups element. Indicates where to look when parsing the plugin through the prefix of the plugin. The pluginGroup element specifies the groupId of the plugin. By default, Maven will automatically put org. Org apache. maven. Plugins. Org codehaus. Mojo# add to pluginGroups.

<pluginGroups>
  <!--plugin Organization of Id(groupId) -->
  <pluginGroup>org.codehaus.mojo</pluginGroup>
</pluginGroups>

1.6,Servers

Function: generally, the download and deployment of the warehouse is in POM Defined in the repositories and distribution management elements in the XML file. However, generally, information such as user name and password (some warehouse access requires security authentication) should not be in POM XML file. This information can be configured in settings XML.

<!--Configure some server settings. Some settings, such as security certificates, should not be associated with pom.xml Distributed together. This type of information should exist on the build server settings.xml File. -->
<servers>
  <!--The server element contains the information needed to configure the server -->
  <server>
    <!--This is server of id(Note that the user is not logged in id),Should id And distributionManagement in repository Elemental id Match. -->
    <id>server001</id>
    <!--Authentication user name. The authentication user name and authentication password represent the login name and password required for server authentication. -->
    <username>my_login</username>
    <!--Authentication password. The authentication user name and authentication password represent the login name and password required for server authentication. Password encryption has been added to 2.1.0 +. For details, please visit the password encryption page -->
    <password>my_password</password>
    <!--The location of the private key used for authentication. Similar to the first two elements, the private key location and private key password specify the path of a private key (the default is ${user.home}/.ssh/id_dsa)And if necessary, a secret word. future passphrase and password Elements may be extracted externally, but for now they must be settings.xml The document is declared in plain text. -->
    <privateKey>${usr.home}/.ssh/id_dsa</privateKey>
    <!--The private key password used for authentication. -->
    <passphrase>some_passphrase</passphrase>
    <!--Permissions when the file is created. If a warehouse file or directory will be created during deployment, permissions can be used at this time( permission). The legal value of these two elements is a three digit number, which corresponds to unix File system permissions, such as 664, or 775. -->
    <filePermissions>664</filePermissions>
    <!--Permissions when the directory is created. -->
    <directoryPermissions>775</directoryPermissions>
  </server>
</servers>

1.7,Mirrors

Function: used to define a series of remote warehouse images. We can define a remote repository in pom to be used when downloading artifacts. However, sometimes the remote warehouse is busy, so people want to create an image to relieve the pressure of the remote warehouse, that is, they will convert the request for the remote warehouse to the request for its image address. Each remote warehouse will have an id, so that we can create our own mirror to associate with the warehouse. When we need to download artifacts from the remote warehouse in the future, Maven can download them from the defined mirror site, which can well alleviate the pressure of our remote warehouse. In the mirror defined by us, each remote warehouse can only have one mirror associated with it, that is, you cannot configure the mirrorOf multiple mirrors to point to the same repository id at the same time.

<mirrors>
  <!-- Download Image of the given warehouse. -->
  <mirror>
    <!-- Unique identifier of the mirror. id Used to distinguish between different mirror Element. -->
    <id>mirrorId</id>
    <!-- Image name -->
    <name>PlanetMirror Australia</name>
    <!-- Of the mirror URL. Building the system will give priority to the use of this URL,Instead of using the default server URL.  -->
    <url>http://downloads.planetmirror.com/pub/maven2</url>
    <!-- Of the mirrored server id. For example, if we want to set up a Maven Central warehouse( http://repo.maven.apache.org/maven2 /), you need to set this element to central. This must be exactly the same as the id central of the central warehouse. -- >
    <mirrorOf>repositoryId</mirrorOf>
  </mirror>
</mirrors>

1.8,Proxies

Function: used to configure different agents.

<proxies>
  <!--The agent element contains the information needed to configure the agent -->
  <proxy>
    <!--The unique definer of the proxy, which is used to distinguish different proxy elements. -->
    <id>myproxy</id>
    <!--Whether the agent is the active one. true Then activate the agent. This element is useful when we declare a set of agents and only need to activate one agent at a time. -->
    <active>true</active>
    <!--Agency agreement. agreement://Hostname: port, separated into discrete elements for easy configuration. -- >
    <protocol>http</protocol>
    <!--The host name of the agent. agreement://Hostname: port, separated into discrete elements for easy configuration. -- >
    <host>proxy.somewhere.com</host>
    <!--The port of the agent. agreement://Hostname: port, separated into discrete elements for easy configuration. -- >
    <port>8080</port>
    <!--The user name, user name and password of the agent represent the login name and password authenticated by the proxy server. -->
    <username>proxyuser</username>
    <!--The password of the proxy. The user name and password represent the login name and password authenticated by the proxy server. -->
    <password>somepassword</password>
    <!--List of host names that should not be proxied. The separator of the list is specified by the proxy server; The vertical bar separator is used in the example, and comma separation is also common. -->
    <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
  </proxy>
</proxies>

1.9,Profiles

Function: adjust the list of build configurations according to environment parameters.
settings. The profile element in XML is POM A cropped version of the profile element in XML. It contains the id, activation, repositories, pluginRepositories, and properties elements. The profile element here only contains these five sub elements because it only cares about building the system as a whole (which is the role orientation of the settings.xml file), rather than individual project object model settings. If a setting The profile in XML is activated, and its value overrides any other defined in pom.xml A profile with the same id in the XML. This profile will be activated when all constraints are met.

<profiles>
    <profile>
  <!-- profile Unique identification of -->
        <id>test</id>     
        <!-- Automatic trigger profile Conditional logic -->
        <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>
        <!-- Extended attribute list -->
        <properties />
        <!-- Remote warehouse list -->
        <repositories />
        <!-- List of plug-in warehouses -->
        <pluginRepositories />
      ...
    </profile>
</profiles>

1.9.1,Activation

Function: automatically trigger the conditional logic of the profile. This is the most important element in the profile. With POM Like the profile in XML, settings The profile in XML can also change some values in specific environments, which are specified through the activation element. The activation element is not the only way to activate a profile. settings. The activeProfile element in the XML file can contain the id of the profile. Profile can also be explicitly activated by using - P tag and comma separated list on the command line (for example, - P test).
jdk: indicates that it is activated when the jdk version meets the conditions. In this case, it is 1.6. The version here can also be represented by a range, such as

< JDK > [1.4,1.7) < / JDK > indicates that 1.4, 1.5 and 1.6 are satisfied;
< JDK > [1.4,1.7] < / JDK > indicates that 1.4, 1.5, 1.6 and 1.7 are satisfied;

os: indicates that it is activated when the operating system meets the conditions.
Property: property is in the form of a key value pair, which means that Maven will activate the profile when it detects such a key value pair.

(1) The following example shows that the profile is activated when the attribute hello exists.

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

(2) The following example shows that the profile is activated when the value of the attribute hello is world.

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

At this time, if you want to activate the profile, you can add the parameter hello when calling Maven instruction and specify its value as world, such as:

mvn compile –Dhello=world

File: indicates that the file is activated when it exists or does not exist, exists indicates that it exists, and missing indicates that it does not exist. The following example shows that the profile is activated when the file hello/world does not exist.

<profile>
    <activation>
        <file>
            <missing>hello/world</missing>
        </file>
    </activation>
</profile>

activeByDefault: when the value is true, it means that if no other profile is activated, the profile will be activated automatically.
Properties: used to define property key value pairs. When the profile is active, the properties specified under properties can be displayed in POM Used in XML. List of extended properties corresponding to the profile.
The maven attribute, like the attribute in ant, can be used to store some values. These values can be found in POM Use the tag ${X} anywhere in XML, where X refers to the name of the attribute. There are five different forms of attributes, all of which can be found in settings XML file.

<!--
  1. env.X: Add before a variable"env."A prefix is returned shell Environment variables. for example,"env.PATH"Refers to $path Environment variables (in Windows It is%PATH%). 
  2. project.x: Refers to POM The corresponding element value in. for example: <project><version>1.0</version></project>adopt ${project.version}get version Value of.
  3. settings.x: Refers to settings.xml The value of the corresponding element in the. For example:<settings><offline>false</offline></settings>adopt ${settings.offline}get offline Value of.
  4. Java System Properties: All available java.lang.System.getProperties()The accessed properties can be POM Use this form of access in, for example ${java.home}. 
  5. x: stay<properties/>Element, or in an external file, to ${someVar}Use in the form of.
 -->
<properties>
    <user.install>${user.home}/our-project</user.install>
</properties>

Note: if the profile is activated, it can be in POM Use ${user.install} in XML.

repositories: used to define the remote warehouse. When the profile is active, the remote warehouse defined here will be used as the remote warehouse of the current pom. It is a set of remote warehouses that maven uses to populate the local warehouse used to build the system.

<repositories>
  <!--Contains information that needs to be connected to the remote warehouse -->
  <repository>
    <!--Unique identification of remote warehouse -->
    <id>codehausSnapshots</id>
    <!--Remote warehouse name -->
    <name>Codehaus Snapshots</name>
    <!--How to handle the download of release version in remote warehouse -->
    <releases>
      <!--true perhaps false Indicates whether the warehouse is enabled for downloading certain types of components (release version, snapshot version). -->
      <enabled>false</enabled>
      <!--This element specifies how often updates occur. Maven Will compare local POM And remote POM The timestamp of the. The options here are: always(All the time), daily(Default, daily), interval: X(here X Is the time interval in minutes), or never(Never). -->
      <updatePolicy>always</updatePolicy>
      <!--When Maven What to do when verifying the component verification file fails-ignore(Ignore), fail(Failed), or warn(Warning). -->
      <checksumPolicy>warn</checksumPolicy>
    </releases>
    <!--How to handle the download of snapshot version in remote warehouse. Yes releases and snapshots These two sets of configurations, POM You can adopt different strategies for each type of component in each separate warehouse. For example, someone may decide to turn on support for snapshot version download only for development purposes. See repositories/repository/releases element -->
    <snapshots>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    </snapshots>
    <!--Remote warehouse URL,Press protocol://hostname/path form -- >
    <url>http://snapshots.maven.codehaus.org/maven2</url>
    <!--Warehouse layout type used to locate and sort components-Can be default(Default) or legacy(Legacy). Maven 2 Provides a default layout for its warehouse; However, Maven 1.x There is a different layout. We can use this element to specify that the layout is default(Default) or legacy(Legacy). -->
    <layout>default</layout>
  </repository>
</repositories>

(1) releases, snapshots: This is a limitation on the type of artifact.
(2) enabled: indicates whether this type of artifact is allowed in this warehouse
(3) updatePolicy: indicates how often updates are attempted. The optional values are always, daily, interval:minutes (indicating how often to update) and never.
(4) checksumPolicy: when Maven deploys the project to the warehouse, it will be submitted together with the verification file. checksumPolicy indicates how to deal with the missing or incorrect verification file. The options are ignore, fail and warn.
Plugin repositories: there are two types of repositories in Maven. One is the repository for storing artifacts, and the other is the repository for storing plugin plug-ins. The definition of plugin repositories is similar to that of repositories. It indicates where Maven can find the required plug-ins. Similar to repository, repository is the repository for managing jar packages, and pluginRepositories is the repository for managing plug-ins. The Maven plug-in is a special type of component. For this reason, the plug-in repository is independent of other repositories. The structure of the pluginRepositories element is similar to that of the repositories element. Each pluginRepository element specifies a remote address that Maven can use to find new plug-ins.

<pluginRepositories>
  <!-- Contains information about the need to connect to the remote plug-in repository.See profiles/profile/repositories/repository Description of the element -->
  <pluginRepository>
    <releases>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    </releases>
    <snapshots>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    </snapshots>
    <id />
    <name />
    <url />
    <layout />
  </pluginRepository>
</pluginRepositories>

Example:

<activation>
  <!--profile Default active ID -->
  <activeByDefault>false</activeByDefault>
  <!--When matched jdk Detected, profile Activated. For example, 1.4 activation JDK1.4,1.4.0_2,and!1.4 Activate all versions not in 1.4 initial  JDK.  -->
  <jdk>1.5</jdk>
  <!--When a matching operating system attribute is detected, profile Activated. os Element can define some operating system related attributes. -->
  <os>
    <!--activation profile The name of the operating system -->
    <name>Windows XP</name>
    <!--activation profile Family of operating systems(as 'windows') -->
    <family>Windows</family>
    <!--activation profile Operating system architecture -->
    <arch>x86</arch>
    <!--activation profile Operating system version of -->
    <version>5.1.2600</version>
  </os>
  <!--If Maven A property has been detected whose value can be POM Passed in ${name}Reference), which has the corresponding name = Value, Profile Will be activated. If the value field is empty, the existing attribute name field is activated profile,Otherwise, attribute value fields are matched case sensitive -->
  <property>
    <!--activation profile The name of the property -->
    <name>mavenVersion</name>
    <!--activation profile The value of the property -->
    <value>2.0.3</value>
  </property>
  <!--Provide a file name to activate by detecting the presence or absence of the file profile. missing Check whether the file exists, and activate if it does not exist profile. on the other hand, exists The file is checked for existence and activated if it exists profile.  -->
  <file>
    <!--Activate if the specified file exists profile.  -->
    <exists>${basedir}/file2.properties</exists>
    <!--Activate if the specified file does not exist profile.  -->
    <missing>${basedir}/file1.properties</missing>
  </file>
</activation>

1.10,ActiveProfiles

Function: manually activate the list of profiles and define active profiles in the order in which profiles are applied.
This element contains a set of active profile elements, and each active profile contains a profile id. Any profile id defined in activeProfile will be activated regardless of the environment settings. If there is no matching profile, nothing will happen.
For example, if env test is an active profile, it is in POM The profile of the corresponding id in the XML (or profile.xml) will be activated. If such a profile cannot be found during operation, Maven will run as usual.

<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
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <activeProfiles>
    <!-- To activate profile id -->
    <activeProfile>env-test</activeProfile>
  </activeProfiles>
  ...
</settings>

Topics: Java Maven