- 1 引言
- 2. settings.xml 元素详解
- 2.1 根元素settings概览
- 2.1.1 LocalRepository
- 2.1.2 InteractiveMode
- 2.1.3 UsePluginRegistry
- 2.1.4 Offline
- 2.1.5 PluginGroups
- 2.1.6 Servers(☆☆☆☆☆)
- 2.1.7 Mirrors(☆☆☆☆☆)
- 2.1.8 Proxies
- 2.1.9 Profiles
- 2.1.10 Activation
- 2.1.11 properties
- 2.1.12 Repositories
- 2.1.13 pluginRepositories
- 2.1.14 ActiveProfiles
Maven
有一个settings.xml
配置文件,它是用来设置 Maven
参数的配置文件,settings.xml
中包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置,这个文件几乎很少修改,但有时是需要修改,比如:我们想替换远程仓库源,下面来学习一下这个文件。
注意:
settings.xml 是 Maven 的全局配置文件
pom.xml 文件是本地项目配置文件
settings.xml
文件一般存在于两个位置:
- 全局配置: ${maven.home}/conf/settings.xml
- 用户配置: ${user.home}/.m2/settings.xml
注意:用户配置优先于全局配置。${user.home}
和所有其他系统属性只能在 3.0+版本上使用。请注意 windows 和 Linux 使用变量的区别。
配置优先级:局部配置优先于全局配置。
配置优先级从高到低:pom.xml > user settings > global settings
如果这些文件同时存在,在应用配置时,会合并它们的内容,如果有重复的配置,优先级高的配置会覆盖优先级低的。
2. settings.xml 元素详解 2.1 根元素settings概览下面列举了settings.xml
中的根元素
2.1.1 LocalRepository
作用:该值表示构建系统本地仓库的路径。其默认值:~/.m2/repository
${user.home}/.m2/repository
2.1.2 InteractiveMode作用:表示 Maven
是否需要和用户交互以获得输入。
如果Maven
需要和用户交互以获得输入,则设置成 true
,反之则应为 false
。默认为 true
。
true
作用:Maven
是否需要使用plugin-registry.xml
文件来管理插件版本。
如果需要让 Maven
使用文件~/.m2/plugin-registry.xml
来管理插件版本,则设为 true
。默认为 false
。
false
作用:表示 Maven
是否需要在离线模式下运行。
如果构建系统需要在离线模式下运行,则为 true
,默认为 false
。 当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。
false
作用:当插件的组织 id(groupId)
没有显式提供时,提供搜寻插件组织 Id(groupId)
的列表。
该元素包含一个 pluginGroup
元素列表,每个子元素包含了一个组织 Id(groupId)
。 当我们使用某个插件,并且没有在命令行为其提供组织 Id(groupId)
的时候,Maven
就会使用该列表。
默认情况下该列表包含了 org.apache.maven.plugins
和 org.codehaus.mojo
。
...
org.codehaus.mojo
...
2.1.6 Servers(☆☆☆☆☆)
作用:仓库的下载和部署是在pom.xml
文件中的 repositories
和 distributionManagement
元素中定义的。然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在 pom.xml
文件中配置,这些信息可以配置在 settings.xml
中。
...
server001
my_login
my_password
${usr.home}/.ssh/id_dsa
some_passphrase
664
775
...
2.1.7 Mirrors(☆☆☆☆☆)
作用:为仓库列表配置的下载镜像列表。
...
planetmirror.com
PlanetMirror Australia
http://downloads.planetmirror.com/pub/maven2
central
...
2.1.8 Proxies
作用:用来配置不同的代理。
...
myproxy
true
http
proxy.somewhere.com
8080
proxyuser
somepassword
*.google.com|ibiblio.org
...
2.1.9 Profiles
作用:根据环境参数来调整构建配置的列表。
settings.xml
中的 profile
元素是pom.xml
中 profile
元素的裁剪版本。
它包含了id
、activation
、repositories
、pluginRepositories
和 properties
元素。
这里的 profile
元素只包含这五个子元素,是因为这里只关心构建系统这个整体(这正是 settings.xml
文件的角色定位),而非单独的项目对象模型设置。如果一个 settings.xml
中的 profile
被激活,它的值会覆盖任何其它定义在 pom.xml
中带有相同 id
的 profile
。
...
test
...
2.1.10 Activation
作用:自动触发 profile 的条件逻辑。
如 pom.xml
中的 profile
一样,profile
的作用在于它能够在某些特定的环境中自动使用某些特定的值;这些环境通过 activation
元素指定。
activation
元素并不是激活 profile
的唯一方式。settings.xml
文件中的 activeProfile
元素可以包含 profile
的 id
。profile
也可以通过在命令行,使用 -P
标记和逗号分隔的列表来显式的激活(如,-P test
)。
false
1.5
Windows XP
Windows
x86
5.1.2600
mavenVersion
2.0.3
${basedir}/file2.properties
${basedir}/file1.properties
注:在 Maven
工程的 pom.xml
所在目录下执行 mvn help:active-profiles
命令可以查看中央仓储的 profile
是否在工程中生效。
作用:对应profile的扩展属性列表。
Maven
属性和 ant
中的属性一样,可以用来存放一些值。这些值可以在 pom.xml
中的任何地方使用标记${X}
来使用,这里X
是指属性的名称。属性有五种不同的形式,并且都能在settings.xml
文件中访问。
env.X
: 在一个变量前加上"env."
的前缀,会返回一个shell
环境变量。例如,"env.PATH"
指代了$path环境变量
(在Windows
上是%PATH%
)project.x
:指代了POM
中对应的元素值。例如: 1.0
通过${project.version}
获得version
的值settings.x
:指代了settings.xml
中对应元素的值。例如:false
通过${settings.offline}
获得offline
的值Java System Properties
:所有可通过java.lang.System.getProperties()
访问的属性都能在POM
中使用该形式访问,例如${java.home}
x
:在元素中,或者外部文件中设置,以
${someVar}
的形式使用
${user.home}/our-project
注:如果该 profile 被激活,则可以在pom.xml中使用${user.install}。
2.1.12 Repositories作用:远程仓库列表,它是 Maven
用来填充构建系统本地仓库所使用的一组远程仓库。
codehausSnapshots
Codehaus Snapshots
false
always
warn
http://snapshots.maven.codehaus.org/maven2
default
2.1.13 pluginRepositories
作用:发现插件的远程仓库列表。
和 repository
类似,只是 repository
是管理 jar
包依赖的仓库,pluginRepositories
则是管理插件的仓库。maven
插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories
元素的结构和 repositories
元素的结构类似。每个 pluginRepository
元素指定一个 Maven
可以用来寻找新插件的远程地址。
2.1.14 ActiveProfiles
作用:手动激活 profiles 的列表,按照profile被应用的顺序定义activeProfile。
该元素包含了一组 activeProfile
元素,每个 activeProfile
都含有一个 profile id
。任何在 activeProfile
中定义的 profile id
,不论环境设置如何,其对应的 profile
都会被激活。如果没有匹配的 profile
,则什么都不会发生。
例如,env-test
是一个 activeProfile
,则在 pom.xml
(或者 profile.xml
)中对应 id
的 profile
会被激活。如果运行过程中找不到这样一个 profile
,Maven
则会像往常一样运行。
...
env-test
...