settings.xml详解(很详细读这一篇就够了)

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
可观测可视化 Grafana 版,10个用户账号 1个月
简介: settings.xml是Java项目中用于配置Maven的重要文件,它详细规定了Maven的运行规则和行为。该文件通常位于用户家目录下的.m2文件夹中,或者项目根目录下的.mvn文件夹内。settings.xml中包含了众多配置项,从代理设置、镜像仓库配置,到服务器认证信息、插件组等,均可以在此文件中进行细致定义。通过合理配置settings.xml,我们可以优化Maven的依赖下载速度,保障仓库访问的安全性,甚至实现私有仓库的搭建与管理。深入了解settings.xml的每一项配置,对于提高Maven使用效率、保障项目构建稳定性具有重要意义。因此,无论是Maven初学者还是资深用户,都应仔

 一、settings.xml在哪里配置,有什么用?

settings.xml文件存在于两个位置

其中一个目录是Maven安装目录下(全局配置):${maven.home}/conf/setting.xml(可以用 mvn -v 命令查看安装的目录位置)

另一个目录是用户目录下(用户级配置):${user.home}/.m2/settings.xml(本查  ~/.m2/settings.xml)

作用:设置Maven参数的配置文件

配置优先级从高到低:项目pom.xml > user settings > global settings

二、settings.xml元素详解

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>/Users/***/***/repos</localRepository>
    <interactiveMode>true</interactiveMode>
    <usePluginRegistry>false</usePluginRegistry>
    <offline>false</offline>
  
    <pluginGroups>
      <pluginGroup>org.mortbay.jetty</pluginGroup>
    </pluginGroups>
    <servers>
        <server>
            <id>nexus_server_id</id>
            <username>my_login</username>
            <password>my_password</password>
            <privateKey>${user.home}/.ssh/id_dsa</privateKey>
            <passphrase>some_passphrase</passphrase>
            <filePermissions>664</filePermissions>
            <directoryPermissions>775</directoryPermissions>
            <configuration></configuration>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <id>nexus-aliyun</id>
            <name>Nexus aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>
        <proxies>
          <proxy>
             <id>myproxy</id>
             <active>true</active>
             <protocol>http</protocol>
             <host>proxy.somewhere.com</host>
             <port>8080</port>
             <username>proxyuser</username>
             <password>somepassword</password>
             <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
         </proxy>
        </proxies>
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
              <activeByDefault>false</activeByDefault>
              <jdk>1.5</jdk>
              <os>
                <name>Windows XP</name>
                <family>Windows</family>
                <arch>x86</arch>
                <version>5.1.2600</version>
              </os>
              <property>
                <name>mavenVersion</name>
                <value>2.0.3</value>
              </property>
              <file>
                <exists>${basedir}/file2.properties</exists>
                <missing>${basedir}/file1.properties</missing>
              </file>
            </activation>
            <repositories>
                <repository>
                    <id>ccl-nexus</id>
                    <url>http://*.*.*.*:8081/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                        <checksumPolicy>warn</checksumPolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                        <checksumPolicy>fail</checksumPolicy>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>ccl-nexus</id>
                    <url>http://*.*.*.*:8081/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
              <test.jdbc.url>
                jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
              </test.jdbc.url>
              <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</test.jdbc.driverClassName>
              <test.jdbc.username>root</test.jdbc.username>
              <test.jdbc.password></test.jdbc.password>
              <test.host.url>http://*.*.*.*:80</test.host.url>
            </properties>
        </profile>     
    </profiles>
    <activeProfiles>
        <activeProfile>dev</activeProfile>
    </activeProfiles>
   
</settings>

image.gif

2.1 LocalRepository

构建系统本地仓库的路径。其默认值为~/.m2/repository

<!-- 默认值是${user.home}/.m2/repository -->
<localRepository>E:/project/localRepository</localRepository>

image.gif

2.2 InteractiveMode

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

如果为false,命令如下 mvn archetype:generate -DgroupId=com.zworks -DartifactId=maven-setting -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

需要指定groupId、artifactId、archetypeArtifactId,如果不指定会报错,因为这些是无法推测出值的。

如果为true,命令如下 mvn archetype:generate

后面会让你选择或输入archetype、groupId、artifactId、version、package、为false的时候之所以不用指定version和package是因为这两个都有默认值。

<interactiveMode>true</interactiveMode>

image.gif

2.3 UsePluginRegistry

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

<!-- 如果Maven使用${user.home}/.m2/plugin-registry.xml来管理plugin的版本,就设置为true,默认为false -->
<usePluginRegistry>false</usePluginRegistry>

image.gif

2.4 Offline

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

<!-- 如果构建系统要在离线模式下工作,设置为true,默认为false。如果构建服务器因为网络故障或者安全问题不能与远程仓库相连,那么这个设置是非常有用的。 -->
<offline>false</offline>

image.gif

2.5 PluginGroups

当插件的组织Id(groupId)没有显式提供时,供搜寻插件组织Id(groupId)的列表。该元素包含一个pluginGroup元素列表,每个子元素包含了一个组织Id(groupId)。当我们使用某个插件,并且没有在命令行为其提供组织Id(groupId)的时候,Maven就会使用该列表。默认情况下该列表包含了org.apache.maven.plugins和org.codehaus.mojo

<!--插件组
    在pluginGroups元素下面可以定义一系列的pluginGroup元素。表示当通过plugin的前缀来解析plugin的时候到哪里寻找。
    pluginGroup元素指定的是plugin的groupId。默认情况下,Maven会自动把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。-->
    <pluginGroups>
      <pluginGroup>org.mortbay.jetty</pluginGroup>
    </pluginGroups>
    <!--例如,有了上面的配置,Maven命令行可以使用简单的命令执行org.morbay.jetty:jetty-maven-plugin:run,如下:
    mvn jetty run
    -->

image.gif

2.6 Servers

用来下载和部署的仓库是用POM中的repositories和distributionManagement元素来定义的。 但是某些配置例如username和password就不应该随着pom.xml来分配了。这种类型的信息应该保存在构建服务器中的settings.xml中。

<!--服务器
用来下载和部署的仓库是用POM中的repositories和distributionManagement元素来定义的。
但是某些配置例如username和password就不应该随着pom.xml来分配了。这种类型的信息应该保存在构建服务器中的settings.xml中。
-->
<servers>
    <server>
    <!-- 这是Server的ID(不是登录进来的user),与Maven想要连接上的repository/mirror中的id元素相匹配。 -->
        <id>nexus_server_id</id>
        <username>my_login</username>
        <password>my_password</password>
    <!-- 与前两个元素一样,这两个成对出现,分别指向了一个私钥(默认的${user.home}/.ssh/id_dsa)和一个passphrase。即分别表示私钥位置和私钥密码 -->
        <privateKey>${user.home}/.ssh/id_dsa</privateKey>
        <passphrase>some_passphrase</passphrase>
    <!-- 文件和目录被创建时的权限。后续需要用此权限来访问。
            这两个元素合法的值是一个三位数字,其对应了unix文件系统的权限,如664,或者775。 -->
        <filePermissions>664</filePermissions>
        <directoryPermissions>775</directoryPermissions>
    <!-- 传输层额外的配置项 -->
        <configuration></configuration>
    </server>
</servers>

image.gif

2.7 Mirrors

为仓库列表配置的下载镜像列表

<mirrors>
    <mirror>
        <!-- 镜像标识id -->
        <id>nexus-aliyun</id>
        <name>Nexus aliyun</name>
        <url>https://maven.aliyun.com/repository/public</url>
        <!-- 指向此镜像的仓库Id,任何对于远程仓库的请求都会被转至此url。
                    多个逗号隔开,或者*号统配,或者!排除某个之外的所有仓库
        external:*匹配除使用 localhost 或基于文件的存储库之外的所有存储库。当您想要排除为集成测试定义的重定向存储库时使用。
        自 Maven 3.8.0 起,external:http:*匹配所有使用 HTTP 的存储库,但使用 localhost 的存储库除外。
        * = 一切
        external:* = 一切不在本地主机上,也不基于文件。
        repo,repo1 = repo或repo1
        *,!repo1 = 除了 repo1 之外的所有东西
            注意不要在逗号分隔列表中的标识符或通配符周围包含额外的空格。
            例如,<mirrorOf设置为 >的镜像!repo1, *不会镜像任何内容,而!repo1,*会镜像除repo1
            注意多个mirrorOf内容相同并不会都生效,每个仓库只能有一个镜像,Maven 不会聚合镜像,而只是选择第一个匹配项
            -->
        <mirrorOf>*</mirrorOf>
    </mirror>
</mirrors>

image.gif

2.8 Proxies

代理设置,主要用于无法直接访问中心的库用户配置。用来配置不同的代理,多代理profiles 可以应对笔记本或移动设备的工作环境:通过简单的设置profile id就可以很容易的更换整个代理配置。

<proxies>
  <proxy>
     <id>myproxy</id>
     <active>true</active>
     <protocol>http</protocol>
     <host>proxy.somewhere.com</host>
     <port>8080</port>
     <!-- 两个元素成对出现,提供连接proxy服务器时的认证 -->
     <username>proxyuser</username>
     <password>somepassword</password>
     <!--不该被代理的主机名列表。该列表的分隔符由代理服务器指定;例子中使用了竖线分隔符,使用逗号分隔也很常见。-->
     <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
 </proxy>
</proxies>

image.gif

2.9 Profiles

settings.xml中的profile是pom.xml中的profile的简洁形式。 它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。 profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。 如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。

<!--配置文件
    settings.xml中的profile是pom.xml中的profile的简洁形式。
    它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。
    profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。
    如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。
    -->
<profiles>
    <profile>
        <id>dev</id>
        <!--自动触发profile的条件逻辑。Activation是profile的开启钥匙。
            如POM中的profile一样,profile的力量来自于它能够在某些特定的环境中自动使用某些特定的值;
            这些环境通过activation元素指定。activation元素并不是激活profile的唯一方式。
            settings.xml文件中的activeProfile元素可以设置需要激活profile的id。
            profile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。-->
          <activation>
          <!-- 默认激活的标识 -->
          <activeByDefault>false</activeByDefault>
          <!--当匹配的jdk被检测到,profile被激活。例如,1.4激活JDK1.4,1.4.0_2,而!1.4激活所有版本不是以1.4开头的JDK。-->
          <jdk>1.5</jdk>
          <!--当匹配的操作系统属性被检测到,profile被激活。os元素可以定义一些操作系统相关的属性。-->
          <os>
            <!--激活profile的操作系统的名字 -->
            <name>Windows XP</name>
            <!--激活profile的操作系统所属家族(如 'windows')  -->
            <family>Windows</family>
            <!--激活profile的操作系统体系结构  -->
            <arch>x86</arch>
            <!--激活profile的操作系统版本-->
            <version>5.1.2600</version>
          </os>
          <!--如果Maven检测到某一个属性(其值可以在POM中通过${name}引用),其满足对应的name = 值,Profile就会被激活。
                        如果值字段是空的,那么存在属性名称字段就会激活profile,否则按区分大小写方式匹配属性值字段-->
          <property>
            <name>mavenVersion</name>
            <value>2.0.3</value>
          </property>
          <!--提供一个文件名,通过检测该文件的存在或不存在来激活profile。missing检查文件是否存在,如果不存在则激活profile。
                        另一方面,exists则会检查文件是否存在,如果存在则激活profile。-->
          <file>
            <!--如果指定的文件存在,则激活profile。 -->
            <exists>${basedir}/file2.properties</exists>
            <!--如果指定的文件不存在,则激活profile。-->
            <missing>${basedir}/file1.properties</missing>
          </file>
        </activation>
        <!--如果以上所有指定的条件都达到了,那么,activation就被触发,而且不需要一次性全部达到。-->
        <!--仓库(repositories)
        仓库是Maven用来构筑构建系统的本地仓库的远程项目集合。它来自于被Maven叫做插件和依赖的本地仓库。
                    不同的远程仓库包含不同的项目,当profile被激活,他们就会需找匹配的release或者snapshot构件。 -->
        
        <!--插件仓库(plugin repositories)
        仓库包含了两种重要类型的构件:第一种是用来做其他构件依赖的构件,这是在中央仓库中的大多数插件。另外一种类型的构件就是插件。
                    Maven的插件本身就是一种特殊的构件。因此,插件仓库被从其他仓库中分离出来。
                    pluginRepositories元素模块的结构与repositories模块很相似。pluginRepository元素指向一个可以找到新插件的远程地址。
        -->
    </profile>
</profiles>

image.gif

2.10 Activation

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

<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>

image.gif

2.11 properties

Maven的属性是值占位符,就像Ant中的属性。如果X是一个属性的话,那么它的值在POM中可以使用${X}来进行任意地方的访问。他们来自于五种不同的风格,所有都可以从settings.xml文件中访问到。

  • env.X:使用“env.”前缀将会返回当前的环境变量。例如${env.PATH}就是使用了$path环境变量。
  • project.X:一个点“.”分割的路径,在POM中就是相关的元素的值。例如:<project><version>1.0</version></project>就可以通过${project.version}来访问。
  • settings.X:一个点“.”分割的路径,在settings.xml中就是相对应的元素的值,例如:<settings><offline>false</offline></settings>就可以通过${settings.offline}来访问。
  • Java系统属性:所有通过java.lang.System.getProperties()来访问的属性都可以像POM中的属性一样访问,例如:${java.home}
  • X:被<properties/>或者外部文件定义的属性,值可以这样访问${someVar}
<properties>
  <test.jdbc.url>
    jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
  </test.jdbc.url>
  <test.jdbc.driverClassName>com.mysql.jdbc.Driver</test.jdbc.driverClassName>
  <test.jdbc.username>root</test.jdbc.username>
  <test.jdbc.password></test.jdbc.password>
  <test.host.url>http://172.16.11.43:80</test.host.url>
</properties>

image.gif

2.12 Repositories

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

<repositories>
    <repository>
        <id>ccl-nexus</id>
        <url>http://172.16.10.99:8081/nexus/content/groups/public</url>
        <!--如何处理远程仓库里发布版本的下载-->
        <releases>
            <!--true或者false表示该仓库是否为下载某种类型构件(发布版,快照版)开启。  -->
            <enabled>true</enabled>
            <!--该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。
                选项是:always(一直),
                daily(默认,每日),
                interval:X(这里X是以分钟为单位的时间间隔),
                never(从不)。 -->
            <updatePolicy>always</updatePolicy>
            <!--当Maven验证构件校验文件失败时该怎么做-ignore(忽略),fail(失败),或者warn(警告)。-->
            <checksumPolicy>warn</checksumPolicy>
        </releases>
        <!--如何处理远程仓库里快照版本的下载。有了releases和snapshots这两组配置,
            POM就可以在每个单独的仓库中,为每种类型的构件采取不同的策略。
            例如,可能有人会决定只为开发目的开启对快照版本下载的支持。参见repositories/repository/releases元素-->
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>fail</checksumPolicy>
        </snapshots>
    </repository>
</repositories>

image.gif

2.13 pluginRepositories

发现插件的远程仓库列表。仓库是两种主要构件的家。第一种构件被用作其它构件的依赖。这是中央仓库中存储的大部分构件类型。另外一种构件类型是插件。Maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories元素的结构和repositories元素的结构类似。每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。

<!--插件仓库(plugin repositories)
仓库包含了两种重要类型的构件:第一种是用来做其他构件依赖的构件,这是在中央仓库中的大多数插件。另外一种类型的构件就是插件。
            Maven的插件本身就是一种特殊的构件。因此,插件仓库被从其他仓库中分离出来。
            pluginRepositories元素模块的结构与repositories模块很相似。pluginRepository元素指向一个可以找到新插件的远程地址。
-->
<pluginRepositories>
    <pluginRepository>
        <id>ccl-nexus</id>
        <url>http://127.0.0.1:8081/nexus/content/groups/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

image.gif

2.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则会像往常一样运行。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5月前
|
XML C# 图形学
【Unity 3D】C#从XML中写入、读取、修改数据(附源码)
【Unity 3D】C#从XML中写入、读取、修改数据(附源码)
37 0
|
9月前
|
XML 数据格式
QXmlStreamReader 读取 XML 文件卡死
如何设置使 QXmlStreamReader 能够读取 XML 文件中特殊符号
90 3
|
XML 数据格式 开发者
使用 Schema 的 sax 方式操作 xml(一)| 学习笔记
快速学习使用 Schema 的 sax 方式操作 xml。
70 0
使用 Schema 的 sax 方式操作 xml(一)| 学习笔记
|
XML 数据格式 开发者
使用 Schema 的 sax 方式操作 xml(二)| 学习笔记
快速学习使用 Schema 的 sax 方式操作 xml。
78 0
使用 Schema 的 sax 方式操作 xml(二)| 学习笔记
|
XML API 数据格式
QXmlStreamReader和QXmlStreamWriter实现将一个xml文件读取后保存为另一个xml
QXmlStreamReader和QXmlStreamWriter实现将一个xml文件读取后保存为另一个xml
Properties 文件的编写与读取
SpringBoot 中的两大配置 properties yaml
2084 0
|
XML 数据格式
|
存储 Java
JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现一个文件管理器,简单的,但是在此之前,我们先来做点小案例 package com.
1252 0