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


      
      
      
      
      
      
      
      
      
      

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.

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


  ${user.home}/.m2/repository
  true
  false
  false
  ...
  • 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。


  ...
  
    org.mortbay.jetty
  
  ...

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命令:

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文件中。


  ...
  
    
      server001
      my_login
      my_password
      ${user.home}/.ssh/id_dsa
      some_passphrase
      664
      775
      
    
  
  ...
  • 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 element. Otherwise, the key will be ignored.

注意:如果您使用私钥登录到服务器,请确保您忽略了元素。否则,键将被忽略。

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

2.4 Mirrors


  ...
  
    
      planetmirror.com
      PlanetMirror Australia
      http://downloads.planetmirror.com/pub/maven2
      central
    
  
  ...
  • 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 section when connecting to the mirror.
  • id,name:该镜像的唯一标识符和友好名称。这个id用于链接镜像时区分镜像元素以及从中选择相应的凭据
  • 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


  ...
  
    
      myproxy
      true
      http
      proxy.somewhere.com
      8080
      proxyuser
      somepassword
      *.google.com|ibiblio.org
    
  
  ...
  • 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.


  ...
  
    
      test
      
        false
        1.5
        
          Windows XP
          Windows
          x86
          5.1.2600
        
        
          mavenVersion
          2.0.3
        
        
          ${basedir}/file2.properties
          ${basedir}/file1.properties
        
      
      ...
    
  
  ...

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: 1.0 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: false 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 element or an external files, the value may be used as ${someVar}.

  ...
  
    
      ...
      
        ${user.home}/our-project
      
      ...
    
  
  ...

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.


  ...
  
    
      ...
      
        
          codehausSnapshots
          Codehaus Snapshots
          
            false
            always
            warn
          
          
            true
            never
            fail
          
          http://snapshots.maven.codehaus.org/maven2
          default
        
      
      
        ...
      
      ...
    
  
  ...
  • 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


  ...
  
    env-test
  

  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.

Maven学习笔记之配置文件(settings.xml)_第1张图片

你可能感兴趣的:(构建工具)