maven远程仓库配置

转:https://blog.csdn.net/qq_35720307/article/details/87088388

配置远程仓库的几种方法:

1 在pom.xml里配置多个远程仓库:

<repositories>    
    <repository>      
       <id>nexus</id>      
       <name>nexus私服URL</name>      
       <url>http://127.0.0.1:8081/repository/maven-public/</url>    
    </repository>    
    <repository>
        <id>aliyun</id>
        <name>阿里云</name> 
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </repository> 
</repositories>

注:配置多个远程仓库时,如果在一个远程找不到,会去其它的远程仓库找(即 多个仓库遍历),所以尽量在这配置多少个远程仓库,默认会有一个ID是central的官方远程仓库。

缺点:每个项目都需要配置,固可以在settings.xml配置全局

2.在settings.xml里配置全局

<profiles>
<profile>
    <id>my</id>
    <repositories>
        <repository>
            <id>aliyun</id>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>   
        <repository>      
            <id>nexus</id>      
            <name>nexus私服URL</name>      
            <url>http://127.0.0.1:8081/repository/maven-public/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>
</profile>
</profiles>
<activeProfiles>
  <activeProfile>my</activeProfile>
</activeProfiles>

配置镜像

镜像主要是用来路由指定ID的远程仓库,即原本从远程仓库拿的jar包全部从镜像去拿(加速下载),mirrorOf用来匹配远程仓库ID,即上面repository的ID

在settings.xml里配置阿里云镜像加速默认的远程仓库

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

全局默认的远程仓库:

<repository>      
       <id>central</id>            
       <url>https://repo1.maven.org/maven2/</url>    
</repository>

注:配置多个镜像时,只会寻找最先匹配的镜像,不会遍历所有镜像

由于配置 <mirrorOf>central</mirrorOf>,所有从center(默认远程仓库)去拉取依赖的 都将 重定向从aliyun镜像中去拉取依赖

注:mirrorOf匹配的都是repository的id

<mirrorOf>*</mirrorOf> :匹配所有仓库请求,即将所有的仓库请求都转到该镜像上
<mirrorOf>repo1,repo2</mirrorOf> :将仓库repo1和repo2的请求转到该镜像上,使用逗号分隔多个远程仓库
<mirrorOf>*,!repo1</miiroOf> : 匹配所有仓库请求,repo1除外(将继续从repo1请求),使用感叹号将仓库从匹配中排除

总结:

1 如果没有nexus私服,可以使用aliyun镜像来加速默认的官方远程仓库

在settings.xml 里配置:

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

2 如果有nexus私服,可以同时使用aliyun镜像和私服远程仓库,aliyun镜像用于加速官方仓库,私服用于加载第三方jar包和自己deploy的jar包

在setting.xml里配置:
 

<mirrors>
        <mirror>
            <id>aliyun</id>
            <mirrorOf>central</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
</mirrors>
<profiles>
<profile>
    <id>my</id>
    <repositories>  
        <repository>      
            <id>nexus</id>      
            <name>nexus私服URL</name>      
            <url>http://127.0.0.1:8081/repository/maven-public/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>
</profile>
</profiles>
<activeProfiles>
  <activeProfile>my</activeProfile>
</activeProfiles>

或者如果私服nexus里可以联网,直接新建repository,设置远程代理到aliyun

然后settings.xml里配置

  <mirrors>
        <mirror>
            <id>nexus</id>
            <mirrorOf>central</mirrorOf>
            <name>Nexus</name>
            <url>http://127.0.0.1:8081/repository/maven-public/</url>
        </mirror>
    </mirrors>

猜你喜欢

转载自blog.csdn.net/qq_30436011/article/details/94378176