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. 龙哥量化:通达信macd黄白线变色公式macd金叉怎么写macd死叉怎么写(需要继续优化,各种变色方式)

    你提出的任何逻辑要求,只要是软件能实现的,我都能用通达信写出来,我レメLong622889通达信.大智慧.文华.博易的编程逻辑差不多,只是个别函数不一样.TB交易开拓者.金字塔和文华8,都是专业的期货 ...

  2. Windows 10 下编译 64 位 OpenJDK 8 并单步调试

    Windows 10 下编译 64 位 OpenJDK 8 软件版本 操作系统:Windows 10 Cygwin:3.5.4-1 Visual Studio:2010 英文版 freetype: 2 ...

  3. Qt数据库应用21-数据分组导出

    一.前言 数据分组导出和打印这个需求并不是近期的需求,而是之前做温湿度监控系统的时候提的需求,当然也有几个系统用到了,比如啤酒保鲜监控系统.这个需求的应用场景是,有很多个设备,每个设备都产生了很多的运 ...

  4. Qt开源作品10-代码统计组件

    一.前言 代码行数统计主要用来统计项目中的所有文件的代码行数,其中包括空行.注释行.代码行,可以指定过滤拓展名,比如只想统计.cpp的文件,也可以指定文件或者指定目录进行统计.写完这个工具第一件事情就 ...

  5. UML之修饰符

    1.可见性修饰符 面向对象思想中有一个重要概念是封装,封装意味着对象中成员的"可见性"是不同的.这里的对象通常指类和包,而它们的可见性通过可见性修饰符进行定义. 在UML中,类对象 ...

  6. DotNetBar控件中,删除或移除AdvTree上指定名称的Node

    废话少说,直接上核心代码: string deleteNodeName = "节点1"; //移除advTree上指定名称的Node Node deleteNode = advTr ...

  7. IM跨平台技术学习(三):vivo的Electron技术栈选型、全方位实践总结

    本文由vivo技术团队Yang Kun分享,原题"electron 应用开发优秀实践",本文有修订. 1.引言 在上篇<Electron初体验(快速开始.跨进程通信.打包.踩 ...

  8. 零基础Windows Server搭建部署Word Press 博客系列教程(1):从萌新到菜鸡之云主机配置与备案

    不知道这个教程能帮助到多少想要长期建站的新手朋友. 下面进入正题.如果你想搭建一个基于wordpress的个人博客或者网站,但是不懂Linux,也没有命令行的操作经验,更不懂复杂的代码,那么找这篇文章 ...

  9. 性能测试工具_nGrinder

    1. ngrinder-controller-3.4.3.war 放置到tomcat的webapps目录下:2. 启动tomcat;3. 访问地址: http://localhost:8080/ngr ...

  10. ASP6.0 (VB) 获取目录下所有图片文件

    <% 'Desc : ASP6.0 (VB) 获取目录下所有图片文件 'by : wgscd 'date : 2024-2-1 dim c_path c_path=Server.MapPath( ...