maven处理配置的优先级顺序

(1)全局settings.xml(优先级★☆☆☆☆)

位于Maven安装目录的conf/settings.xml,提供系统级的默认配置,比如本地仓库位置、远程仓库列表等。这些设置对所有用户和项目都有效,但优先级最低。

(2)用户级别的settings.xml(优先级★★☆☆☆)

位于用户主目录下的.m2/settings.xml,覆盖全局的settings.xml设置。这里的配置仅对当前用户生效,优先级高于全局settings.xml

(3)父项目POM(优先级★★★☆☆)

父项目POM定义了所有子项目的共享配置,如依赖管理(dependencyManagement)、插件管理(pluginManagement)、构建配置等。父POM的设置为所有继承它的子项目提供了基础配置,但是子项目可以直接覆盖这些配置。

(4)子项目POM(优先级★★★★☆)

子项目的POM文件具有最高优先级,它可以覆盖来自父POM的所有配置,包括直接声明依赖、构建属性、插件配置等。子项目的直接配置将优先于任何上级配置。

(5)profiles和profile-specifix配置(优先级★★★★★)

不论是在settings.xml还是POM.xml中,激活的profiles可以进一步调整配置。如果一个profile在多个层级被定义(比如既有用户级settings.xml中的又有项目POM中的),那么激活的profile中定义的设置将根据定义的顺序(局部优先于全局)来覆盖之前的配置。

(6)其他配置文件(如profiles.xml等)(优先级★★★★★)

虽然不常见,但在特定场景下,Maven也允许使用或自定义其他配置文件,比如通过profiles.xml来管理一组可激活的profiles。这些额外的配置同样遵循局部(项目或用户)优先于全局的原则。

总结来说,Maven的配置优先级遵循“局部覆盖全局”和“子项目覆盖父项目”的原则,确保了灵活性和可维护性。开发者可以在更具体的层面(如子项目POM或激活的profiles)上进行精确控制,而不影响到更广泛的配置环境。

一、本地仓库(Local Repository)

指定Maven下载所有依赖包的位置。默认情况下,Maven会在用户目录下的.m2.reposiriey目录存储这些依赖

<localRepository>D:\Environment\mvnrepo</localRepository>

二、镜像(Mirrors)

配置国内maven仓库(阿里云、华为、网易...)的镜像地址,用于替换默认的中央仓库或其他远程仓库,提高依赖下载速度或解决访问受限问题。

<!-- 阿里云maven仓库仓库 -->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/central</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/spring</url>
<mirrorOf>spring</mirrorOf>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/releases</url>
<mirrorOf>releases</mirrorOf>
</mirror> <!-- 华为maven仓库 -->
<mirror>
<id>huaweicloud</id>
<name>mirrorfrommaven huaweicloud</name>
<url>https://repo.huaweicloud.com/repository/maven/</url>
<mirrorOf>central</mirrorOf>
</mirror> <!-- 163 -->
<mirror>
<id>nexus-163</id>
<mirrorOf>*</mirrorOf>
<name>网易 Nexus 163</name>
<url>http://mirrors.163.com/maven/repository/maven-public/</url>
</mirror>

三、代理(Proxies)

如果你的网络环境需要通过代理服务器访问外网,可以在这里配置HTTP、HTTPS或FTP代理

<proxies>
<proxy>
<id>example-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.example.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>proxypass</password>
<nonProxyHosts>localhost|127.0.0.1</nonProxyHosts>
</proxy>
</proxies>

四、服务器(Servers)

配置访问私有仓库或部署时需要认证的服务器凭据。

<servers>
<server>
<id>server-id</id>
<username>your-username</username>
<password>your-password</password>
<!-- Optionally, for more security, you can use encrypted passwords -->
<!--<password>${env.MAVEN_PASSWORD}</password>-->
</server>
</servers>

五、全局属性(Properties)

可以在<properties>标签内定义一些全局变量,这些变量可以在POM文件中引用。

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

我的settings.xml(D:\Environment\apache-maven-3.8.7\conf\settings.xml

settings.xml
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
--> <!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.conf}/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\Environment\mvnrepo</localRepository> <!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
--> <!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
--> <!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups> <!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies> <!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
--> <!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers> <!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- <mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>public</mirrorOf>
</mirror> --> <!-- 阿里云maven仓库仓库 -->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/central</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/spring</url>
<mirrorOf>spring</mirrorOf>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/releases</url>
<mirrorOf>releases</mirrorOf>
</mirror> <!-- 华为maven仓库 -->
<mirror>
<id>huaweicloud</id>
<name>mirrorfrommaven huaweicloud</name>
<url>https://repo.huaweicloud.com/repository/maven/</url>
<mirrorOf>central</mirrorOf>
</mirror> <!-- 163 -->
<mirror>
<id>nexus-163</id>
<mirrorOf>*</mirrorOf>
<name>网易 Nexus 163</name>
<url>http://mirrors.163.com/maven/repository/maven-public/</url>
</mirror> <!-- junit镜像地址
<mirror>
<id>junit</id>
<name>junit Address/</name>
<url>http://jcenter.bintray.com/</url>
<mirrorOf>central</mirrorOf>
</mirror>
-->
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
</mirrors> <!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id> <activation>
<jdk>1.4</jdk>
</activation> <repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!-- jdk1.8 -->
<profile>
<!-- profile的唯一标识 -->
<id>jdk-1.8</id>
<!-- 自动触发profile的条件逻辑 -->
<activation>
<!-- 当其值为true的时候表示如果没有其他的profile处于激活状态的时候,该profile将自动被激活。 -->
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<!-- 扩展属性列表 -->
<!-- 用于定义属性键值对的。当该profile是激活状态的时候,properties下面指定的属性都可以在pom.xml中使用。 -->
<!--如果Maven检测到某一个属性(其值可以在POM中通过${name}引用),
其拥有对应的name = 值,Profile就会被激活。如果值字段是空的,那么存在属性名称字段就会激活profile,
否则按区分大小写方式匹配属性值字段 -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile> <!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id> <activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation> <properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
</profiles> <!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>

— 业精于勤荒于嬉,行成于思毁于随 —

maven - [02] settings.xml配置的更多相关文章

  1. Maven的settings.xml配置详解

    子节点详细介绍转载:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension 全局配置 ...

  2. Maven 的 settings.xml 配置中的mirror节点

    maven2的setting.xml大家都知道,里面有个mirrors节点,用来配置镜像URL. mirrors可以配置多个mirror,每个mirror有id,name,url,mirrorOf属性 ...

  3. Maven settings.xml配置解读

    本文对${maven.home}\conf\settings.xml的官方文档作个简单的解读,请确保自己的maven环境安装成功,具体安装流程详见Maven安装 第一步:看settings.xml的内 ...

  4. Maven——settings.xml配置

    settings.xml配置 原文 <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed ...

  5. Maven settings.xml配置(指定本地仓库、阿里云镜像设置)

    转: 详解Maven settings.xml配置(指定本地仓库.阿里云镜像设置) 更新时间:2018年12月18日 11:14:45   作者:AmaniZ   我要评论   一.settings. ...

  6. Maven项目使用Nexus作为远程仓库的settings.xml配置

    Maven项目使用Nexus作为远程仓库的settings.xml配置(转) 在自己电脑C:\Users\hanmm\.m2\下的setting.xml. 1.服务器配置 <server> ...

  7. [java][JEECG] Maven settings.xml JEECG项目初始化 RouYi settings.xml配置

    好吧一下是经验之谈,原本这些坑不应该躺的,从头看手册完全可以避免这些. 懒得整理了,看懂了就看,看不懂自己琢磨JEECG的帮助文档去,不过嘛我喜欢用Intelij IDEA,他里面都是别的IDE,不喜 ...

  8. Maven学习存档(2)——settings.xml配置

    二.settings.xml配置 2.1 原文 <?xml version="1.0" encoding="UTF-8"?> <!-- Lic ...

  9. Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):3、Maven独立插件安装与settings.xml配置

    文章目录: Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):1.JIRA账号注册 Taurus.MVC-Java 版本打包上传到Maven中央仓库(详细过程):2.PGP ...

  10. IDEA设置maven修改settings.xml配置文件无法加载仓库

    作为初学者配置maven一般网上搜索.然后你就看到各种配置文件片段,首先配置镜像,然后配置仓库.完事后再IDEA里面配置下maven的路径和配置文件路径. 这些文章属实坑爹,完全没讲一个重要的配置就是 ...

随机推荐

  1. k8s强制删除pod节点

    场景 突然get pod的时候,发现一堆的错误,得把它干掉,否则很不爽. 解决方案 正常过期的状态,比如Evicted ,用正常指令 kubectl -n jingu get pods | grep ...

  2. ES6 面试题

    新增了哪些属性? 新增块级作用域:let.const 新增数据类型:Symbol 表示独一无二的值 新增数据结构: Set,类似数组,所有数据是唯一的: Map,键值对的结合,传统的 Object 只 ...

  3. ASP.NET Core 中的 Request Feature

    ASP.NET Core 中的 Request Feature https://docs.microsoft.com/en-us/aspnet/core/fundamentals/request-fe ...

  4. Powershell 源码批判

    代码里充斥着过程式编程的搞法:比如这里 Utils.PathIsUnc,分散的到处都是 internal static IEnumerable<string> GetDefaultAvai ...

  5. windows 也支持右键复制文件名了

    mac 有一个操作,alt + 右键,出现的菜单有复制路径一项.不用羡慕,现在 windows 也有这个功能了. Shift + 右键,"复制为路径":

  6. DDD你真的理解清楚了吗?怎么准确理解“值对象”

    这些年,随着软件业的不断发展,软件系统开始变得越来越复杂而难于维护.这时,越来越多的开发团队开始选择实践DDD领域驱动设计.领域驱动设计是一种非常优秀的软件设计思想,它可以非常好地帮助我们梳理复杂业务 ...

  7. Qt编写地图综合应用13-获取边界点

    一.前言 获取边界点一般和行政区划搭配起来使用,比如用户输入一个省市的名称,然后自动定位到该省市,然后对该轮廓获取所有边界点集合输出到js文件,最后供离线使用,获取边界点还有一个功能就是获取当前区域内 ...

  8. 解决Failed to load module canberra-gtk-module错误

    在Ubuntu环境里,通过./triangulation 1.png 2.png 命令运行高翔的ch7的triangulation程序时报错: Gtk-Message: 09:10:26.571: F ...

  9. [转]来,让我们一起来盘盘 Nodejs 环境变量(process.env)

    首先 process.env 是什么? node环境变量: process 是node的全局变量,类似浏览器的window: env 是process的一个属性. 官方解释:process 对象是一个 ...

  10. tomcat源码分析(二)如何处理请求

    概述 tomcat的核心就是处理请求, 接收Request, 建立Socket链接, 处理,返回Response. 通过前面的架构图可以知道每个Service都包括连接器Connector组件和容器C ...