maven 下载jar包加载顺序

在 Maven 构建过程中,依赖的下载源取决于你的 pom.xml 文件中的 配置、settings.xml 文件中的 和 配置,以及你的 Nexus 仓库的设置。以下是决定 Maven 从哪个仓库下载依赖的关键点:

仓库配置优先级

  1. 项目 pom.xml 文件中的仓库配置:优先使用在项目 pom.xml 文件中指定的仓库。
  2. 用户 settings.xml 文件中的仓库配置:如果项目中没有指定仓库,则使用用户 settings.xml 文件中的配置。
  3. 全局 settings.xml 文件中的仓库配置:如果用户 settings.xml 中也没有配置,则使用全局 settings.xml
    文件中的配置(通常位于 Maven 的安装目录中)。

仓库的匹配和下载逻辑

  1. 定义的仓库顺序:Maven 会按照仓库定义的顺序依次查询。第一个找到所需依赖的仓库将被使用。
  2. 镜像配置:如果在 settings.xml 中定义了镜像(),所有对原始仓库的请求会被重定向到镜像仓库。
  3. 本地缓存:在查询远程仓库之前,Maven 首先会在本地缓存(~/.m2/repository)中查找依赖。

示例配置和行为

pom.xml

xml 复制代码
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-project</artifactId>
    <version>1.0.0</version>
    
    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
        </repository>
        <repository>
            <id>nexus-releases</id>
            <url>http://nexus.mxcd.top/repository/maven-releases/</url>
        </repository>
    </repositories>
</project>

settings.xml

xml 复制代码
<settings>
    <mirrors>
        <mirror>
            <id>aliyun-repos</id>
            <name>aliyun repository</name>
            <mirrorOf>central</mirrorOf>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>mxc-release-repo</id>
                    <url>http://nexus.mxcd.top/repository/maven-releases/</url>
                </repository>
                <repository>
                    <id>mxc-snapshot-repo</id>
                    <url>http://nexus.mxcd.top/repository/maven-snapshots/</url>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>mxc-release-repo</id>
                    <url>http://nexus.mxcd.top/repository/maven-releases/</url>
                </pluginRepository>
                <pluginRepository>
                    <id>mxc-snapshot-repo</id>
                    <url>http://nexus.mxcd.top/repository/maven-snapshots/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>

        <profile>
            <id>aliyun</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
</settings>

分析下载行为

  1. 镜像配置生效:
  • 如果你在 settings.xml 中配置了镜像 aliyun-repos,且指定 了
    central,那么所有对中央仓库的请求都会被重定向到阿里云的镜像仓库。这意味着,任何试图从中央仓库下载的依赖都会从阿里云镜像仓库下载。
  1. 项目 pom.xml 中的仓库配置:
  • 在你的 pom.xml 文件中,如果你明确指定了 nexus-releases 仓库,那么该仓库会首先被查询。如果依赖在 nexus-releases 仓库中存在,它会从该仓库下载。
  • 如果 nexus-releases 仓库中没有找到依赖,接下来会查询中央仓库(被重定向到阿里云镜像仓库)。
  1. 用户 settings.xml 中的 Profile 配置:

示例操作流程

假设你的项目 pom.xml 文件和 settings.xml 文件配置如下:

xml 复制代码
pom.xml
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-project</artifactId>
    <version>1.0.0</version>
    
    <repositories>
        <repository>
            <id>nexus-releases</id>
            <url>http://nexus.mxcd.top/repository/maven-releases/</url>
        </repository>
    </repositories>
</project>
xml 复制代码
settings.xml

<settings>
    <mirrors>
        <mirror>
            <id>aliyun-repos</id>
            <name>aliyun repository</name>
            <mirrorOf>central</mirrorOf>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>mxc-release-repo</id>
                    <url>http://nexus.mxcd.top/repository/maven-releases/</url>
                </repository>
                <repository>
                    <id>mxc-snapshot-repo</id>
                    <url>http://nexus.mxcd.top/repository/maven-snapshots/</url>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>mxc-release-repo</id>
                    <url>http://nexus.mxcd.top/repository/maven-releases/</url>
                </pluginRepository>
                <pluginRepository>
                    <id>mxc-snapshot-repo</id>
                    <url>http://nexus.mxcd.top/repository/maven-snapshots/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>

        <profile>
            <id>aliyun</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
</settings>

行为总结

具体操作示例

假设你希望从 Nexus 私库下载依赖,但如果私库中没有找到依赖再从阿里云镜像下载,确保你没有在命令行中指定 profile,则 nexus profile 会被自动激活,行为如下:

bash 复制代码
sh
mvn clean install
  1. 查询 Nexus 私库:首先查询 http://nexus.mxcd.top/repository/maven-releases/http://nexus.mxcd.top/repository/maven-snapshots/。
  2. 查询阿里云镜像:如果私库中没有找到依赖,查询 http://maven.aliyun.com/nexus/content/groups/public。

这样配置可以保证在不同的构建场景下灵活选择依赖下载的源,同时提高构建过程中的效率和稳定性。

相关推荐
云间月131415 分钟前
飞算JavaAI智慧文旅场景实践:从景区管理到游客服务的全链路系统搭建
java·开发语言
盖世英雄酱5813615 分钟前
必须掌握的【InheritableThreadLocal】
java·后端
找不到、了21 分钟前
JVM的逃逸分析深入学习
java·jvm
用户03321266636735 分钟前
Java 查找并替换 PDF 中的文本:高效自动化处理指南
java
叽哥1 小时前
Kotlin学习第 1 课:Kotlin 入门准备:搭建学习环境与认知基础
android·java·kotlin
Hy行者勇哥1 小时前
物联网软件开发过程中,数据流图(DFD),用例图,类图,活动图,序列图,状态图,实体关系图(ERD),BPMN(业务流程建模)详解分析
java·物联网·struts
Miracle6581 小时前
从 0 到 1 开发校园二手交易系统:飞算 JavaAI 全流程实战
java
A尘埃1 小时前
Java+Python混合微服务OCR系统设计
java·python·微服务·混合
Seven972 小时前
剑指offer-22、从上往下打印⼆叉树
java
A尘埃2 小时前
企业级Java项目金融应用领域——保险系统(补充)
java·金融·保险系统