--- title: Maven 教程之 settings.xml 详解 date: 2019-05-14 14:57:33 order: 03 categories: - Java - 软件 - 构建 - Maven tags: - Java - 构建 - Maven permalink: /pages/9919f8ec/ --- # 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`中的顶级元素 ```xml ``` ### LocalRepository **作用**:该值表示构建系统本地仓库的路径。 其默认值:\~/.m2/repository。 ```xml ${user.home}/.m2/repository ``` ### InteractiveMode **作用**:表示 maven 是否需要和用户交互以获得输入。 如果 maven 需要和用户交互以获得输入,则设置成 true,反之则应为 false。默认为 true。 ```xml true ``` ### UsePluginRegistry **作用**:maven 是否需要使用 plugin-registry.xml 文件来管理插件版本。 如果需要让 maven 使用文件\~/.m2/plugin-registry.xml 来管理插件版本,则设为 true。默认为 false。 ```xml false ``` ### Offline **作用**:表示 maven 是否需要在离线模式下运行。 如果构建系统需要在离线模式下运行,则为 true,默认为 false。 当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。 ```xml false ``` ### PluginGroups **作用**:当插件的组织 id(groupId)没有显式提供时,供搜寻插件组织 Id(groupId)的列表。 该元素包含一个 pluginGroup 元素列表,每个子元素包含了一个组织 Id(groupId)。 当我们使用某个插件,并且没有在命令行为其提供组织 Id(groupId)的时候,Maven 就会使用该列表。默认情况下该列表包含了 `org.apache.maven.plugins` 和 `org.codehaus.mojo`。 ```xml ... org.codehaus.mojo ... ``` ### Servers **作用**:一般,仓库的下载和部署是在 pom.xml 文件中的 `repositories` 和 `distributionManagement` 元素中定义的。然而,一般类似用户名、密码(**有些仓库访问是需要安全认证的**)等信息不应该在 pom.xml 文件中配置,这些信息可以配置在 `settings.xml` 中。 ```xml ... server001 my_login my_password ${usr.home}/.ssh/id_dsa some_passphrase 664 775 ... ``` ### Mirrors **作用**:为仓库列表配置的下载镜像列表。 ```xml ... planetmirror.com PlanetMirror Australia http://downloads.planetmirror.com/pub/maven2 central ... ``` ### Proxies **作用**:用来配置不同的代理。 ```xml ... myproxy true http proxy.somewhere.com 8080 proxyuser somepassword *.google.com|ibiblio.org ... ``` ### Profiles **作用**:根据环境参数来调整构建配置的列表。 `settings.xml` 中的 `profile` 元素是 `pom.xml` 中 `profile` 元素的**裁剪版本**。 它包含了`id`、`activation`、`repositories`、`pluginRepositories` 和 `properties` 元素。这里的 profile 元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是 settings.xml 文件的角色定位),而非单独的项目对象模型设置。如果一个 `settings.xml` 中的 `profile` 被激活,它的值会覆盖任何其它定义在 `pom.xml` 中带有相同 id 的 `profile`。 ```xml ... test ... ``` #### Activation **作用**:自动触发 `profile` 的条件逻辑。 如 `pom.xml` 中的 `profile` 一样,`profile` 的作用在于它能够在某些特定的环境中自动使用某些特定的值;这些环境通过 `activation` 元素指定。 `activation` 元素并不是激活 `profile` 的唯一方式。`settings.xml` 文件中的 `activeProfile` 元素可以包含 `profile` 的 `id`。`profile` 也可以通过在命令行,使用 `-P` 标记和逗号分隔的列表来显式的激活(如,`-P test`)。 ```xml 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 是否在工程中生效。 #### properties **作用**:对应`profile`的扩展属性列表。 maven 属性和 ant 中的属性一样,可以用来存放一些值。这些值可以在 `pom.xml` 中的任何地方使用标记\${X}来使用,这里 X 是指属性的名称。属性有五种不同的形式,并且都能在 settings.xml 文件中访问。 ```xml ${user.home}/our-project ``` > 注:如果该 profile 被激活,则可以在`pom.xml`中使用\${user.install}。 #### Repositories **作用**:远程仓库列表,它是 maven 用来填充构建系统本地仓库所使用的一组远程仓库。 ```xml codehausSnapshots Codehaus Snapshots false always warn http://snapshots.maven.codehaus.org/maven2 default ``` #### pluginRepositories **作用**:发现插件的远程仓库列表。 和 `repository` 类似,只是 `repository` 是管理 jar 包依赖的仓库,`pluginRepositories` 则是管理插件的仓库。 maven 插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。`pluginRepositories` 元素的结构和 `repositories` 元素的结构类似。每个 `pluginRepository` 元素指定一个 Maven 可以用来寻找新插件的远程地址。 ```xml ``` ### ActiveProfiles **作用**:手动激活 profiles 的列表,按照`profile`被应用的顺序定义`activeProfile`。 该元素包含了一组 `activeProfile` 元素,每个 `activeProfile` 都含有一个 profile id。任何在 `activeProfile` 中定义的 profile id,不论环境设置如何,其对应的 `profile` 都会被激活。如果没有匹配的 `profile`,则什么都不会发生。 例如,env-test 是一个 activeProfile,则在 pom.xml(或者 profile.xml)中对应 id 的 profile 会被激活。如果运行过程中找不到这样一个 profile,Maven 则会像往常一样运行。 ```xml ... env-test ... ``` 至此,maven settings.xml 中的标签都讲解完毕,希望对大家有所帮助。 ## 参考资料 - [maven 官方文档之 settings](https://maven.apache.org/settings.html)