Maven配置优先级(总结)

本文详细介绍了Maven配置文件settings.xml的优先级、如何查看生效配置以及阿里云镜像代理的具体配置方法,帮助开发者理解Maven配置原理,解决jar包下载问题。

一、前言

公司内部研发项目时,通常会将一些通用工具类封装成jar包并上传到自己的Maven私服中,这些jar包可能只在公司内网提供。那我们不在公司时,可能需要下载一些其它jar包,我们可以选用阿里云作为Maven镜像代理地址。在切换Maven配置文件settings.xml时,一定要搞清楚配置文件的生效情况,否则不合理的配置可能会导致某些jar包下载不下来!

二、settings.xml配置文件优先级

优先级:pom.xml> user settings > global settings

说明

  • 全局配置: ${M2_HOME}/conf/settings.xml(Eclipse或Idea开发工具可配置)
  • 用户配置: user.home/.m2/settings.xml(Eclipse或Idea开发工具可配置)
  • 局部配置优先于全局配置
  • 若这些文件同时存在,在应用配置时,会合并它们的内容,如果有重复的配置,优先级高的配置会覆盖优先级低的。(合并覆盖原则)

三、查看生效配置

以Idea开发工具为例:

1.选中项目=>右键=>Run Maven=>New Goal…

Run-Maven

2.输入help:effective-settings,点击运行,控制台会输出合并、覆盖之后最终的settings.xml文件。
3.输入help:active-profiles,点击运行,控制台会输出生效的profiles;Idea工具Maven窗口会直接显示profiles生效情况。

四、阿里云镜像代理settings.xml

说明:建议直接放于 ${M2_HOME}/conf目录,覆盖掉原有settings.xml文件,作为默认的全局配置。公司内部的私有仓库,单独设置一个settings.xml,配置于Idea的User settings file输入框,作为合并、覆盖配置。

<?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">
 
    <!-- 默认的值是${user.home}/.m2/repository -->
    <localRepository>D:\maven\aliyun-repository</localRepository>
 
    <!-- 如果Maven要试图与用户交互来得到输入就设置为true,否则就设置为false,默认为true。 -->
    <interactiveMode>true</interactiveMode>
 
    <!-- 如果Maven使用${user.home}/.m2/plugin-registry.xml来管理plugin的版本,就设置为true,默认为false。 -->
    <usePluginRegistry>false</usePluginRegistry>
 
    <!-- 如果构建系统要在离线模式下工作,设置为true,默认为false。 如果构建服务器因为网络故障或者安全问题不能与远程仓库相连,那么这个设置是非常有用的。 -->
    <offline>false</offline>
 
 <mirrors>

    <mirror>
        <id>nexus-aliyun</id>
		<mirrorOf>central,spring-milestone,spring-plugins-release,spring-libs-release,spring-snapshot</mirrorOf>
        <name>Nexus aliyun</name>
        <url>https://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>   
     
  </mirrors>
 
    <!-- settings.xml中的profile是pom.xml中的profile的简洁形式。 它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。 
        profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。 如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。 -->
    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <repositories>
                <repository>
                    <id>aliyun-public</id>
                    <name>aliyun public</name>
                    <url>https://maven.aliyun.com/repository/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <layout>default</layout>        
                </repository>

                <repository>
                    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬山境KL攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值