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

<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

<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 配置:
  • 如果你在 settings.xml 中激活了 nexus profile,那么在没有其他配置覆盖的情况下,Maven 会优先从 nexus profile 中定义的仓库下载依赖。
  • 例如,如果你在构建命令中使用 mvn clean install -P nexus,Maven 会首先查询 http://nexus.mxcd.top/repository/maven-releases/ 和 http://nexus.mxcd.top/repository/maven-snapshots/ 仓库。

示例操作流程

假设你的项目 pom.xml 文件和 settings.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>
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>

行为总结

  • 默认行为:
    如果你运行 mvn clean install,nexus profile 被自动激活,Maven 会优先从 http://nexus.mxcd.top/repository/maven-releases/http://nexus.mxcd.top/repository/maven-snapshots/ 下载依赖。

  • 中央仓库镜像:
    如果 nexus profile 中定义的仓库没有找到依赖,且依赖定义在中央仓库,那么由于镜像配置,Maven 会从 http://maven.aliyun.com/nexus/content/groups/public 下载。

  • 命令行指定 profile:
    如果你运行 mvn clean install -P aliyun,则会激活 aliyun profile,Maven 只会从 http://maven.aliyun.com/nexus/content/groups/public 下载依赖,而不会查询 nexus 仓库。

具体操作示例

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

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。

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

### 如何将从 Maven 官网下载JAR 手动添加到 IntelliJ IDEA 项目中 在某些特殊场景下,可能需要手动将从 Maven 官网或其他来源下载JAR 导入到 IntelliJ IDEA 中。以下是具体方法: #### 方法一:通过项目的 `lib` 文件夹引入 1. 创建一个新的文件夹用于存储外部 JAR ,通常命名为 `lib` 并放置于项目根目录下。 2. 将下载好的 JAR 复制到该文件夹中。 3. 打开 IntelliJ IDEA 的 **Project Structure** 设置窗口(快捷键为 `Ctrl+Alt+Shift+S` 或者菜单栏路径为 `File -> Project Structure...`)。 4. 在左侧导航栏选择 **Modules**,然后选中目标模块并切换至 **Dependencies** 标签页。 5. 点击右侧的加号按钮 (`+`),选择 **JARs or directories**,找到之前创建的 `lib` 文件夹并将其中的 JAR 加入。 6. 调整新添加的 JAR 顺序以确保其优先级满足需求。 此操作完成后,IntelliJ IDEA 即可识别这些外部库,并允许你在代码中使用它们的功能[^1]。 #### 方法二:利用 Maven 的本地安装功能 如果希望更正式地管理这个 JAR ,则可以考虑将其安装到本地 Maven 仓库中: 1. 使用命令行工具进入JAR 的位置; 2. 输入如下指令完成安装: ```bash mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \ -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar ``` 替换 `<path-to-file>`、`<group-id>`、`<artifact-id>` 和 `<version>` 参数为你实际使用的值。 3. 修改当前项目的 `pom.xml` 文件,在 `<dependencies>` 部分增加对应条目描述刚才安装的组件版本信息。 4. 刷新 Maven 工程让更改生效(可以通过右键单击工程名或者直接点击 IDE 右侧边栏上的刷新按钮实现)。 这样做的好处是可以像其他远程托管的依赖项一样管理和更新它[^2]。 --- ```xml <!-- Example of adding dependency into pom.xml --> <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0.0</version> </dependency> ``` 上述 XML 片段展示了如何定义一个普通的 Maven 依赖关系[^3]。 --- #### 注意事项 - 如果发现加载速度较慢,可能是由于网络连接不稳定造成的。尝试调整代理设置或更换更快捷镜像站点地址解决这个问题。 - 对于 Spring Boot 类型的应用程序来说,默认已经集成了很多常用框架的支持,因此除非必要情况外一般不需要额外单独处理第三方类库的情况[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值