Java教程

maven设置多个镜像源,按顺序下载依赖

本文主要是介绍maven设置多个镜像源,按顺序下载依赖,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目标

分别配置两个nexus镜像源,目的: 编译时,先从nexus1尝试下载依赖,如果失败,到nexus2下载依赖,再失败,去公网中央仓库下载镜像

配置

settings.xml

<?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">

    <mirrors>
        <mirror>
            <id>nexus1</id>
            <mirrorOf>nexus1</mirrorOf>
            <url>http://nexus1.com/nexus/repository/maven-delivery/</url>
        </mirror>
        <mirror>
            <id>nexus2</id>
            <mirrorOf>nexus2</mirrorOf>
            <url>http://nexus2.com/repository/maven-public/</url>
        </mirror>
    </mirrors>

    <!-- 配置连接密钥,如果需要认证 -->
    <servers>
        <server>
            <id>nexus1</id>
            <username>demo</username>
            <password>demo001</password>
        </server>
    </servers>

    <!-- The resolution of multiple Repositories only works with profiles!-->
    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>nexus1</id>
                    <url>http://nexus1.com/nexus/repository/maven-delivery/</url>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                </repository>
                <repository>
                    <id>nexus2</id>
                    <url>http://nexus2.com/repository/maven-public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus1</id>
                    <url>http://nexus1.com/nexus/repository/maven-delivery/</url>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                </pluginRepository>
                <pluginRepository>
                    <id>nexus2</id>
                    <url>http://nexus2.com/repository/maven-public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>

</settings>

测试

使用mvn插件下载一个不存在的依赖,查看效果

mvn dependency:get -DgroupId=com.github.ggdefe -DartifactId=ggdefe -Dversion=1.0.0

输出


                    
这篇关于maven设置多个镜像源,按顺序下载依赖的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!