Maven学习笔记之配置文件(settings.xml)

来源:互联网 发布:王鹏博士 杨浦云计算 编辑:程序博客网 时间:2024/06/05 02:20

译文: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(下面是罗列出了所有的顶级元素):

[html] view plain copy
print?
  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.                           https://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>  
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.

顶层设置的一半元素都是简单的值,其代表了用于构建系统的全局值。

[html] view plain copy
print?
  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.                       https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   <localRepository>${user.home}/.m2/repository</localRepository>  
  6.   <interactiveMode>true</interactiveMode>  
  7.   <usePluginRegistry>false</usePluginRegistry>  
  8.   <offline>false</offline>  
  9.   ...  
  10. </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。

[html] view plain copy
print?
  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.                       https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   ...  
  6.   <pluginGroups>  
  7.     <pluginGroup>org.mortbay.jetty</pluginGroup>  
  8.   </pluginGroups>  
  9.   ...  
  10. </settings>  
For example, given the above settings the Maven command line may executeorg.mortbay.jetty:jetty-maven-plugin:run with the truncated command:
例如,鉴于上述设置,Maven控制台可以通过简短命令来执行org.mortbay.jetty:jetty-maven-plugin:run命令:

[plain] view plain copy
print?
  1. 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文件中。

[html] view plain copy
print?
  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.                       https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   ...  
  6.   <servers>  
  7.     <server>  
  8.       <id>server001</id>  
  9.       <username>my_login</username>  
  10.       <password>my_password</password>  
  11.       <privateKey>${user.home}/.ssh/id_dsa</privateKey>  
  12.       <passphrase>some_passphrase</passphrase>  
  13.       <filePermissions>664</filePermissions>  
  14.       <directoryPermissions>775</directoryPermissions>  
  15.       <configuration></configuration>  
  16.     </server>  
  17.   </servers>  
  18.   ...  
  19. </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

[html] view plain copy
print?
  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.                       https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   ...  
  6.   <mirrors>  
  7.     <mirror>  
  8.       <id>planetmirror.com</id>  
  9.       <name>PlanetMirror Australia</name>  
  10.       <url>http://downloads.planetmirror.com/pub/maven2</url>  
  11.       <mirrorOf>central</mirrorOf>  
  12.     </mirror>  
  13.   </mirrors>  
  14.   ...  
  15. </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

[html] view plain copy
print?
  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.                       https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   ...  
  6.   <proxies>  
  7.     <proxy>  
  8.       <id>myproxy</id>  
  9.       <active>true</active>  
  10.       <protocol>http</protocol>  
  11.       <host>proxy.somewhere.com</host>  
  12.       <port>8080</port>  
  13.       <username>proxyuser</username>  
  14.       <password>somepassword</password>  
  15.       <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>  
  16.     </proxy>  
  17.   </proxies>  
  18.   ...  
  19. </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.

[html] view plain copy
print?
  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.                       https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   ...  
  6.   <profiles>  
  7.     <profile>  
  8.       <id>test</id>  
  9.       <activation>  
  10.         <activeByDefault>false</activeByDefault>  
  11.         <jdk>1.5</jdk>  
  12.         <os>  
  13.           <name>Windows XP</name>  
  14.           <family>Windows</family>  
  15.           <arch>x86</arch>  
  16.           <version>5.1.2600</version>  
  17.         </os>  
  18.         <property>  
  19.           <name>mavenVersion</name>  
  20.           <value>2.0.3</value>  
  21.         </property>  
  22.         <file>  
  23.           <exists>${basedir}/file2.properties</exists>  
  24.           <missing>${basedir}/file1.properties</missing>  
  25.         </file>  
  26.       </activation>  
  27.       ...  
  28.     </profile>  
  29.   </profiles>  
  30.   ...  
  31. </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.

[plain] view plain copy
print?
  1. 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}.

[html] view plain copy
print?
  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.                       https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   ...  
  6.   <profiles>  
  7.     <profile>  
  8.       ...  
  9.       <properties>  
  10.         <user.install>${user.home}/our-project</user.install>  
  11.       </properties>  
  12.       ...  
  13.     </profile>  
  14.   </profiles>  
  15.   ...  
  16. </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.

[html] view plain copy
print?
  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.                       https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   ...  
  6.   <profiles>  
  7.     <profile>  
  8.       ...  
  9.       <repositories>  
  10.         <repository>  
  11.           <id>codehausSnapshots</id>  
  12.           <name>Codehaus Snapshots</name>  
  13.           <releases>  
  14.             <enabled>false</enabled>  
  15.             <updatePolicy>always</updatePolicy>  
  16.             <checksumPolicy>warn</checksumPolicy>  
  17.           </releases>  
  18.           <snapshots>  
  19.             <enabled>true</enabled>  
  20.             <updatePolicy>never</updatePolicy>  
  21.             <checksumPolicy>fail</checksumPolicy>  
  22.           </snapshots>  
  23.           <url>http://snapshots.maven.codehaus.org/maven2</url>  
  24.           <layout>default</layout>  
  25.         </repository>  
  26.       </repositories>  
  27.       <pluginRepositories>  
  28.         ...  
  29.       </pluginRepositories>  
  30.       ...  
  31.     </profile>  
  32.   </profiles>  
  33.   ...  
  34. </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

[html] view plain copy
print?
  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.                       https://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   ...  
  6.   <activeProfiles>  
  7.     <activeProfile>env-test</activeProfile>  
  8.   </activeProfiles>  
  9. </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
  这篇博客里面是我对于我读过的,并且感觉有意义的文章的收集整理,纯粹的个人爱好,大家感觉有兴趣的可以阅读一下,我也会时常的对其进行更新。
********************************************************************************感谢********************************************************************************************

原创粉丝点击