Maven 之 settings.xml 详解

本文详细介绍了Maven的全局配置文件settings.xml的作用、位置及其配置优先级。内容包括settings.xml中的LocalRepository、InteractiveMode、UsePluginRegistry等顶级元素的解释,以及如何设置本地仓库、离线模式和代理。此外,还解析了profiles、repositories和pluginRepositories等关键配置项,帮助理解Maven的全局配置管理。
摘要由CSDN通过智能技术生成

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中的顶级元素

<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/repository</localRepository>
InteractiveMode

作用:表示 maven 是否需要和用户交互以获得输入。

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

<interactiveMode>true</interactiveMode>
UsePluginRegistry

作用:maven 是否需要使用 plugin-registry.xml 文件来管理插件版本。

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

<usePluginRegistry>false</usePluginRegistry>
Offline

作用:表示 maven 是否需要在离线模式下运行。

如果构建系统需要在离线模式下运行,则为 true,默认为 false。

当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。

<offline>false</offline>
PluginGroups

作用:当插件的组织 id(groupId)没有显式提供时,供搜寻插件组织 Id(groupId)的列表。

该元素包含一个 pluginGroup 元素列表,每个子元素包含了一个组织 Id(groupId)。

当我们使用某个插件,并且没有在命令行为其提供组织 Id(groupId)的时候,Maven 就会使用该列表。默认情况下该列表包含了 org.apache.maven.plugins 和 org.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>
    <!--plugin的组织Id(groupId) -->
    <pluginGroup>org.codehaus.mojo</pluginGroup>
  </pluginGroups>
  ...
</settings>
Servers

作用:一般,仓库的下载和部署是在 pom.xml 文件中的 repositories 和 distributionManagement 元素中定义的。然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在 pom.xml 文件中配置,这些信息可以配置在 settings.xml 中。

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值