Maven 之 settings.xml 详解
一、settings.xml 简介
settings.xml 有什么用
从 settings.xml 的文件名就可以看出,它是用来设置 maven 参数的配置文件。settings.xml 中包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置。
settings.xml 是 maven 的全局配置文件。pom.xml 文件是本地项目配置文件。
settings.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
如果这些文件同时存在,在应用配置时,会合并它们的内容,如果有重复的配置,优先级高的配置会覆盖优先级低的。
二、settings.xml 元素详解
顶级元素概览
下面列举了settings.xml中的顶级元素
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
作用:该值表示构建系统本地仓库的路径。
其默认值:~/.m2/repository。
InteractiveMode
作用:表示 maven 是否需要和用户交互以获得输入。
如果 maven 需要和用户交互以获得输入,则设置成 true,反之则应为 false。默认为 true。
UsePluginRegistry
作用:maven 是否需要使用 plugin-registry.xml 文件来管理插件版本。
如果需要让 maven 使用文件~/.m2/plugin-registry.xml 来管理插件版本,则设为 true。默认为 false。
Offline
作用:表示 maven 是否需要在离线模式下运行。
如果构建系统需要在离线模式下运行,则为 true,默认为 false。
当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。
PluginGroups
作用:当插件的组织 id(groupId)没有显式提供时,供搜寻插件组织 Id(groupId)的列表。
该元素包含一个 pluginGroup 元素列表,每个子元素包含了一个组织 Id(groupId)。
当我们使用某个插件,并且没有在命令行为其提供组织 Id(groupId)的时候,Maven 就会使用该列表。默认情况下该列表包含了 org.apache.maven.plugins 和 org.codehaus.mojo。
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> ... ...
Servers
作用:一般,仓库的下载和部署是在 pom.xml 文件中的 repositories 和 distributionManagement 元素中定义的。然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在 pom.xml 文件中配置,这些信息可以配置在 settings.xml 中。
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> ... ...
Mirrors
作用:为仓库列表配置的下载镜像列表。
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> ... ...
Proxies
作用:用来配置不同的代理。
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> ... ...
Profiles
作用:根据环境参数来调整构建配置的列表。
settings.xml 中的 profile 元素是 pom.xml 中 profile 元素的裁剪版本。
它包含了id、activation、repositories、pluginRepositories 和 properties 元素。这里的 profile 元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是 settings.xml 文件的角色定位),而非单独的项目对象模型设置。如果一个 settings.xml 中的 profile 被激活,它的值会覆盖任何其它定义在 pom.xml 中带有相同 id 的 profile。
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> ... ...
Activation
作用:自动触发 profile 的条件逻辑。
如 pom.xml 中的 profile 一样,profile 的作用在于它能够在某些特定的环境中自动使用某些特定的值;这些环境通过 activation 元素指定。 activation 元素并不是激活 profile 的唯一方式。settings.xml 文件中的 activeProfile 元素可以包含 profile 的 id。profile 也可以通过在命令行,使用 -P 标记和逗号分隔的列表来显式的激活(如,-P test)。
注:在 maven 工程的 pom.xml 所在目录下执行 mvn help:active-profiles 命令可以查看中央仓储的 profile 是否在工程中生效。
properties
作用:对应profile的扩展属性列表。
maven 属性和 ant 中的属性一样,可以用来存放一些值。这些值可以在 pom.xml 中的任何地方使用标记${X}来使用,这里 X 是指属性的名称。属性有五种不同的形式,并且都能在 settings.xml 文件中访问
注:如果该 profile 被激活,则可以在pom.xml中使用${user.install}。
Repositories
作用:远程仓库列表,它是 maven 用来填充构建系统本地仓库所使用的一组远程仓库。
pluginRepositories
作用:发现插件的远程仓库列表。
和 repository 类似,只是 repository 是管理 jar 包依赖的仓库,pluginRepositories 则是管理插件的仓库。 maven 插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories 元素的结构和 repositories 元素的结构类似。每个 pluginRepository 元素指定一个 Maven 可以用来寻找新插件的远程地址。
ActiveProfiles
作用:手动激活 profiles 的列表,按照profile被应用的顺序定义activeProfile。
该元素包含了一组 activeProfile 元素,每个 activeProfile 都含有一个 profile id。任何在 activeProfile 中定义的 profile id,不论环境设置如何,其对应的 profile 都会被激活。如果没有匹配的 profile,则什么都不会发生。
例如,env-test 是一个 activeProfile,则在 pom.xml(或者 profile.xml)中对应 id 的 profile 会被激活。如果运行过程中找不到这样一个 profile,Maven 则会像往常一样运行。
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> ... ...
相关文章
发表评论