maven全局配置文件settings.xml详解

  • 概要
  •     settings.xml有什么用?
  •     settings.xml文件位置
  •     配置优先级
  • settings.xml元素详解
  •     顶级元素概览
  •     LocalRepository
  •     InteractiveMode
  •     UsePluginRegistry
  •     Offline
  •     PluginGroups
  •     Servers
  •     Mirrors
  •     Proxies
  •     Profiles
  •         Activation
  •         properties
  •         Repositories
  •         pluginRepositories
  •     ActiveProfiles
回到顶部

概要

settings.xml有什么用?

如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径。

settings.xml文件是干什么的,为什么要配置它呢?
从settings.xml的文件名就可以看出,它是用来设置maven参数的配置文件。并且,settings.xml是maven的全局配置文件。而pom.xml文件是所在项目的局部配置。
Settings.xml中包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置。

settings.xml文件位置

settings.xml文件一般存在于两个位置:
全局配置: ${M2_HOME}/conf/settings.xml
用户配置: user.home/.m2/settings.xmlnote

{user.home} 和和所有其他系统属性只能在3.0+版本上使用。请注意windows和Linux使用变量的区别。

配置优先级

需要注意的是:局部配置优先于全局配置
配置优先级从高到低:pom.xml> user settings > global settings
如果这些文件同时存在,在应用配置时,会合并它们的内容,如果有重复的配置,优先级高的配置会覆盖优先级低的。

回到顶部

settings.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">
  <localRepository/>
  <interactiveMode/>
  <usePluginRegistry/>
  <offline/>
  <pluginGroups/>
  <servers/>
  <mirrors/>
  <proxies/>
  <profiles/>
  <activeProfiles/>
settings>

LocalRepository

作用:该值表示构建系统本地仓库的路径。
其默认值:~/.m2/repository。

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

InteractiveMode

作用:表示maven是否需要和用户交互以获得输入。
如果maven需要和用户交互以获得输入,则设置成true,反之则应为false。默认为true。

<interactiveMode>trueinteractiveMode>

UsePluginRegistry

作用:maven是否需要使用plugin-registry.xml文件来管理插件版本。
如果需要让maven使用文件~/.m2/plugin-registry.xml来管理插件版本,则设为true。默认为false。

<usePluginRegistry>falseusePluginRegistry>

Offline

作用:表示maven是否需要在离线模式下运行。
如果构建系统需要在离线模式下运行,则为true,默认为false。
当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。

<offline>falseoffline>

PluginGroups

作用:当插件的组织id(groupId)没有显式提供时,供搜寻插件组织Id(groupId)的列表。
该元素包含一个pluginGroup元素列表,每个子元素包含了一个组织Id(groupId)。
当我们使用某个插件,并且没有在命令行为其提供组织Id(groupId)的时候,Maven就会使用该列表。默认情况下该列表包含了org.apache.maven.pluginsorg.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.codehaus.mojopluginGroup>
  pluginGroups>
  ...
settings>

Servers

作用:一般,仓库的下载和部署是在pom.xml文件中的repositoriesdistributionManagement元素中定义的。然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在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>server001id>
      
      <username>my_loginusername>
      
      <password>my_passwordpassword>
      
      <privateKey>${usr.home}/.ssh/id_dsaprivateKey>
      
      <passphrase>some_passphrasepassphrase>
      
      <filePermissions>664filePermissions>
      
      <directoryPermissions>775directoryPermissions>
    server>
  servers>
  ...
settings>

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.comid>
      
      <name>PlanetMirror Australianame>
      
      <url>http://downloads.planetmirror.com/pub/maven2url>
      
      <mirrorOf>centralmirrorOf>
    mirror>
  mirrors>
  ...
settings>

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>myproxyid>
      
      <active>trueactive>
      
      <protocol>httpprotocol>
      
      <host>proxy.somewhere.comhost>
      
      <port>8080port>
      
      <username>proxyuserusername>
      
      <password>somepasswordpassword>
      
      <nonProxyHosts>*.google.com|ibiblio.orgnonProxyHosts>
    proxy>
  proxies>
  ...
settings>

Profiles

作用:根据环境参数来调整构建配置的列表。
settings.xml中的profile元素是pom.xmlprofile元素的裁剪版本
它包含了idactivationrepositoriespluginRepositoriesproperties元素。这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile

<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>testid>
      
      <activation />
      
      <properties />
      
      <repositories />
      
      <pluginRepositories />
    profile>
  profiles>
  ...
settings>

Activation

作用:自动触发profile的条件逻辑。
pom.xml中的profile一样,profile的作用在于它能够在某些特定的环境中自动使用某些特定的值;这些环境通过activation元素指定。
activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profileidprofile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。

<activation>
  
  <activeByDefault>falseactiveByDefault>
  
  <jdk>1.5jdk>
  
  <os>
    
    <name>Windows XPname>
    
    <family>Windowsfamily>
    
    <arch>x86arch>
    
    <version>5.1.2600version>
  os>
  
  <property>
    
    <name>mavenVersionname>
    
    <value>2.0.3value>
  property>
  
  <file>
    
    <exists>${basedir}/file2.propertiesexists>
    
    <missing>${basedir}/file1.propertiesmissing>
  file>
activation>

注:在maven工程的pom.xml所在目录下执行mvn help:active-profiles命令可以查看中央仓储的profile是否在工程中生效。

properties

作用:对应profile的扩展属性列表。
maven属性和ant中的属性一样,可以用来存放一些值。这些值可以在pom.xml中的任何地方使用标记${X}来使用,这里X是指属性的名称。属性有五种不同的形式,并且都能在settings.xml文件中访问。


<properties>
  <user.install>${user.home}/our-projectuser.install>
properties>

注:如果该profile被激活,则可以在pom.xml中使用${user.install}。

Repositories

作用:远程仓库列表,它是maven用来填充构建系统本地仓库所使用的一组远程仓库。

<repositories>
  
  <repository>
    
    <id>codehausSnapshotsid>
    
    <name>Codehaus Snapshotsname>
    
    <releases>
      
      <enabled>falseenabled>
      
      <updatePolicy>alwaysupdatePolicy>
      
      <checksumPolicy>warnchecksumPolicy>
    releases>
    
    <snapshots>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    snapshots>
    
    <url>http://snapshots.maven.codehaus.org/maven2url>
    
    <layout>defaultlayout>
  repository>
repositories>

pluginRepositories

作用:发现插件的远程仓库列表。
repository类似,只是repository是管理jar包依赖的仓库,pluginRepositories则是管理插件的仓库。
maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories元素的结构和repositories元素的结构类似。每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。

<pluginRepositories>
  
  <pluginRepository>
    <releases>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    releases>
    <snapshots>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    snapshots>
    <id />
    <name />
    <url />
    <layout />
  pluginRepository>
pluginRepositories>

ActiveProfiles

作用:手动激活profiles的列表,按照profile被应用的顺序定义activeProfile
该元素包含了一组activeProfile元素,每个activeProfile都含有一个profile id。任何在activeProfile中定义的profile id,不论环境设置如何,其对应的 profile都会被激活。如果没有匹配的profile,则什么都不会发生。
例如,env-test是一个activeProfile,则在pom.xml(或者profile.xml)中对应id的profile会被激活。如果运行过程中找不到这样一个profile,Maven则会像往常一样运行。

<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-testactiveProfile>
  activeProfiles>
  ...
settings>

你可能感兴趣的:(maven,配置相关,maven)