Maven學習筆記之配置文件(settings.xml)


譯文:Documentation for settings.xml

1. Introduction(引言)

1.1 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.

  settings.xml文件中包含的元素用來定義以不同的方式運行Maven的配置,就像pom.xml文件,但是不予具體的項目進行捆綁不應捆綁到任何具體的項目。例如:其中的包含的值有本地倉庫的位置,備用遠程倉庫服務器和身份驗證信息等等。

There are two locations where a settings.xml file may live(settings.xml文件出現的兩個位置):

  • The Maven install: ${maven.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-specific settings.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.

技巧:如果你需要從零開始創建用戶自定義設置,最容易的方式是從Maven安裝目錄下復制全局設置到你的${user.home}/.m2文件夾中。Maven提供的默認的settings.xml文件是包含了意見與實例的模板,因此你可以快速的調整到適合你的需求。

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
https://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( settings.xml中的內容可以使用下面的表達式):

  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.xml不能屬性進行插值。

上面環境變量的使用例子:我們的java的環境變量的名稱為:JAVA_HOME,我們可以使用${env.JAVA_HOME}來引用這個變量值。

2. Settings Details(設置細節)

2.1 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
https://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.
  • localRepository:這個值表示的是建立本地倉庫的路徑。默認的值為:${user.home}/.m2/repository。這個元素在構建一個所有登錄用戶公有的本地存儲庫時非常有用。
  • interactiveMode: true if Maven should attempt to interact with the user for input, false if not. Defaults to true.
  • interactiveMode:默認情況下為true。如果為true則Maven試圖進行交互是允許的,否則不允許。
  • usePluginRegistry: true if Maven should use the ${user.home}/.m2/plugin-registry.xml file to manage plugin versions, defaults to false.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.
  • usePluginRegistry:如果Maven需要使用${user.home}/.m2/plugin-registry.xml文件來管理插件版本則需要設置為true,默認情況下是false。
  • offline: true if this build system should operate in offline mode, defaults to false. This element is useful for build servers which cannot connect to a remote repository, either because of network setup or security reasons.
  • offline:如果構建的系統需要運行在脫機模式下則需要設置為true,默認情況下為false。無論是網絡設置或者安全原因,使用次元素構建一個無法鏈接到遠程倉庫的服務器是很有用的。

2.2 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.

  這個元素包含了很多pluginGroup元素,每一個都包含一個groupId。當使用一個插件並且並沒有在命令行指定其groupId時便會搜索這個列表。這個列表中自動包含org.apache.maven.plugins和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
https://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:
例如,鑒於上述設置,Maven控制台可以通過簡短命令來執行 org.mortbay.jetty:jetty-maven-plugin:run命令:

mvn jetty:run

2.3 Servers

  The repositories for download and deployment are defined by the repositories anddistributionManagementelements of the POM. However, certain settings such asusername andpassword should not be distributed along with the pom.xml. This type of information should exist on the build server in thesettings.xml.
  通過POM的repositories和distributionManagement元素來定義下載和部署倉庫。然而,某些設置,諸如用戶名和密碼不應該隨着pom.xml分布。這種類型的信息應該存放在構建服務器的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
https://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 the id element of the repository/mirror that Maven tries to connect to.
  • id:
  • username, password: These elements appear as a pair denoting the login and password required to authenticate to this server.
  • username, password:這些元素顯示為一對表示需要驗證該服務器的登錄名和密碼。
  • 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 the settings.xml file.
  • privateKey, passphrase:像前兩個元素一樣,
  • 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.
  • filePermissions, directoryPermissions:

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>元素。否則,鍵將被忽略。

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

2.4 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
https://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. The id is used to differentiate betweenmirror elements and to pick the corresponding credentials from the <servers> section when connecting to the mirror.
  • id,name:該鏡像的唯一標識符和友好名稱。這個id用於鏈接鏡像時區分鏡像元素以及從<servers>中選擇相應的憑據
  • 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.
  • url:這個鏡像的基本URL。構建項目將使用這個URL連接倉庫而不是原始的倉庫的URL。
  • mirrorOf: The id of the repository that this is a mirror of.For example, to point to a mirror of the Mavencentral repository (https://repo.maven.apache.org/maven2/), set this element to central. More advanced mappings like repo1,repo2or*,!inhouse are also possible. This must not match the mirror id.
  • mirrorOf:倉庫的id就是一個mirror of。

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

2.5 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
https://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 between proxy elements.
  • id:這個代理的唯一標識。用來區分代理元素。
  • 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.
  • active:true表示這個代理是有效的。聲明一組代理是非常有用的,但是同一時間只可以有一個是有效的。
  • protocol, host, port: The protocol://host:port of the proxy, seperated into discrete elements.
  • protocol,host,prot:代理的protocol://host:port,都是獨立地元素。
  • username, password: These elements appear as a pair denoting the login and password required to authenticate to this proxy server.
  • username,password:這些元素表示為一對表示需要驗證該代理服務器的登錄名和密碼。
  • 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.
  • nonProxyHosts:這是一個不可代理的主機列表。

2.6 Profiles

  The profile element in the settings.xml is a truncated version of thepom.xml profile element. It consists of theactivation, repositories, pluginRepositories andproperties elements. The profileelements only include these four elements because they concerns themselves with the build system as a whole (which is the role of thesettings.xmlfile), not about individual project object model settings.
  settings.xml文件中的profile元素是一個簡短的pom.xml中的profile版本。其包含了activation, repositories, pluginRepositoriesproperties元素。這個profile元素只包含了這四個元素,這是因為其關心的是整個系統的建立(這是settings.xml文件的作用),而不是關於單個項目對象模型的設置。
  If a profile is active from settings, its values will override any equivalently ID’d profiles in a POM orprofiles.xml file.

2.6.1 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
https://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 the jdk 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 the existence of a file, or if it is missing.

The activation element is not the only way that a profile may be activated. The settings.xml file’s activeProfile element may contain the profile’s id. 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

2.6.2 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 the settings.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
https://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.

2.6.3 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
https://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 or snapshots).
  • 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) or never.
  • checksumPolicy: When Maven deploys files to the repository, it also deploys corresponding checksum files. Your options are to ignore, 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.

2.6.4 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 the pluginRepositories element block is similar to the repositories element. The pluginRepository elements each specify a remote location of where Maven can find new plugins.

2.7 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
https://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 of activeProfile 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, if env-test is an activeProfile, a profile in a pom.xml (or profile.xml with a corrosponding id will be active. If no such profile is found then execution will continue as normal.

&#8195&#8195

最后修改時間:2016年11月21日18:06:02

********************************************************************************結束語********************************************************************************************
  我在寫這篇博客的時候也是一名初學者,有任何疑問或問題請留言,或發郵件也可以,郵箱為:577328725@qq.com,我會盡早的進行更正及更改。
在我寫過的博客中有兩篇博客是對資源的整理,可能對大家都有幫助,大家有興趣的話可以看看!!
下載資料整理——目錄http://blog.csdn.net/fanxiaobin577328725/article/details/51894331
  這篇博客里面是我關於我見到的感覺不錯的好資源的整理,里面包含了書籍及源代碼以及個人搜索的一些資源,如果有興趣的可以看看,我會一直對其進行更新和添加。
優秀的文章&優秀的學習網站之收集手冊http://blog.csdn.net/fanxiaobin577328725/article/details/52753638
  這篇博客里面是我對於我讀過的,並且感覺有意義的文章的收集整理,純粹的個人愛好,大家感覺有興趣的可以閱讀一下,我也會時常的對其進行更新。
********************************************************************************感謝********************************************************************************************


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com