(转)maven镜像详解

 背景:一直以来,对maven镜像不是特别的了解,这里通过对网上资料的收集,做个详细的记录。

镜像介绍

如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。换句话说,任何一个可以从仓库Y获得获得的构件,都能从它的镜像中获取。

镜像是为了提供更快的服务

如图:X就认为是Y的一个镜像。

举个例子,http://maven.net.cn/content/groups/public/ 是中央仓库http://repo1.naven.org/maven2/在中国的镜像,由于地理位置的因素,该镜像往往能够提供比中央仓库快得多的服务。

因此可以配置Maven使用该镜像来替代中央仓库。编辑settings.xml如下

复制代码
<settings>  
  ...  
  <mirrors>  
    <mirror>  
      <id>maven.net.cn</id>  
      <name>one of the central mirrors in china</name>  
      <url>http://maven.net.cn/content/groups/public/</url>  
      <mirrorOf>central</mirrorOf>  
    </mirror>  
  </mirrors>  
  ...  
</settings>  
复制代码

该例中,<mirrorOf>的值为central,表示该配置为中央仓库的镜像,任何对于中央仓库的请求都会转至该镜像,用户也可以使用同样的方法配置其他仓库的镜像。

另外三个元素id,name,url与一般仓库配置无异,表示该镜像仓库的唯一标识符、名称以及地址。类似地,如果该镜像需认证,也可以基于该id配置仓库认证

 

任何需要的构件都可以从私服获得,私服就是所有仓库的镜像。这时,可以配置这样的一个镜像,如例:

复制代码
<settings>  
  ...  
  <mirrors>  
    <mirror>  
      <id>internal-repository</id>  
      <name>Internal Repository Manager</name>  
      <url>http://192.168.1.100/maven2</url>  
      <mirrorOf>*</mirrorOf>  
    </mirror>  
  </mirrors>  
  ...  
</settings>  
复制代码

该例中<mirrorOf>的值为星号,表示该配置是所有Maven仓库的镜像,任何对于远程仓库的请求都会被转至http://192.168.1.100/maven2/。

如果该镜像仓库需要认证,则配置一个Id为internal-repository的<server>即可

为了满足一些复杂的需求,Maven还支持更高级的镜像配置:

复制代码
1.<mirrorOf>*</mirrorOf>

匹配所有远程仓库。

2.<mirrorOf>external:*</mirrorOf>

匹配所有远程仓库,使用localhost的除外,使用file://协议的除外。也就是说,匹配所有不在本机上的远程仓库。

3.<mirrorOf>repo1,repo2</mirrorOf>

匹配仓库repo1和repo2,使用逗号分隔多个远程仓库。

4.<mirrorOf>*,!repo1</miiroOf>

匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除。 
复制代码

需要注意的是,由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,Maven仍将无法访问被镜像仓库,因而将无法下载构件。

使用Maven镜像

用maven做项目,最郁闷的莫过于某些依赖库下载不了。被墙了,你懂的。使用maven镜像仓库及其重要,特别是国内的镜像,可以有效缓解被墙疼痛

国外镜像

ibiblio.org

<mirror>  
     <id>ibiblio</id>  
     <mirrorOf>central</mirrorOf>  
     <name>ibiblio Mirror of http://repo1.maven.org/maven2/</name>  
     <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>  
</mirror>  

jboss

<mirror>  
     <id>jboss-public-repository-group</id>  
     <mirrorOf>central</mirrorOf>  
     <name>JBoss Public Repository Group</name>  
     <url>http://repository.jboss.org/nexus/content/groups/public</url>  
</mirror>

repo2

<mirror>
  <id>repo2</id>
  <mirrorOf>central</mirrorOf>
  <name>Human Readable Name for this Mirror.</name>
  <url>http://repo2.maven.org/maven2/</url>
</mirror>

uk.maven.org

<mirror>
  <id>ui</id>
  <mirrorOf>central</mirrorOf>
  <name>Human Readable Name for this Mirror.</name>
 <url>http://uk.maven.org/maven2/</url>
</mirror>

国内镜像

oschina.net

<mirror>
    <id>nexus-osc</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus osc</name>
    <url>http://maven.oschina.net/content/groups/public/</url>
</mirror>

net.cn

<mirror>
    <id>net-cn</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://maven.net.cn/content/groups/public/</url>   
 </mirror>

另一篇博客中的镜像地址:

复制代码
<mirror>  
    <id>repo2</id>  
    <mirrorOf>central</mirrorOf>  
    <name>Human Readable Name for this Mirror.</name>  
    <url>http://repo2.maven.org/maven2/</url>  
</mirror>  
<mirror>  
    <id>net-cn</id>  
    <mirrorOf>central</mirrorOf>  
    <name>Human Readable Name for this Mirror.</name>  
    <url>http://maven.net.cn/content/groups/public/</url>  
</mirror>  
<mirror>  
    <id>ui</id>  
    <mirrorOf>central</mirrorOf>  
    <name>Human Readable Name for this Mirror.</name>  
    <url>http://uk.maven.org/maven2/</url>  
</mirror>  
<mirror>  
    <id>ibiblio</id>  
    <mirrorOf>central</mirrorOf>  
    <name>Human Readable Name for this Mirror.</name>  
    <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>  
</mirror>  
<mirror>  
    <id>jboss-public-repository-group</id>  
    <mirrorOf>central</mirrorOf>  
    <name>JBoss Public Repository Group</name>  
    <url>http://repository.jboss.org/nexus/content/groups/public</url>  
</mirror>  
<mirror>  
    <id>skynet.be</id>  
    <url>http://maven2.mirrors.skynet.be/pub/maven2</url>  
    <mirrorOf>central</mirrorOf>  
</mirror>  
<mirror>  
    <id>cica.es</id>  
    <url>http://ftp.cica.es/mirrors/maven2</url>  
    <mirrorOf>central</mirrorOf>  
</mirror>  
<mirror>  
    <id>ibiblio.org</id>  
    <name>ibiblio Mirror of http://repo1.maven.org/maven2/</name>  
    <url>http://mirrors.ibiblio.org/pub/mirrors/maven2</url>  
    <mirrorOf>central</mirrorOf>  
</mirror>
复制代码

 

使用镜像

下文以oschina.net的镜像为例子.

1.Maven 的安装目录下的 conf 文件下有个settings.xml文件,编辑该文件

2.在<mirrors>中插入

<mirror>
    <id>nexus-osc</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus osc</name>
    <url>http://maven.oschina.net/content/groups/public/</url>
</mirror>

3.这里是配置 Maven 的 mirror 地址指向OSChina 的 Maven 镜像地址。 在执行 Maven 命令的时候, Maven 还需要安装一些插件包,这些插件包的下载地址也让其指向 oschina.net 的 Maven 地址。在<profiles>中插入:

复制代码
<profile>
    <id>jdk-1.4</id>
    <activation>
    <jdk>1.4</jdk>
    </activation>
    <repositories>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://maven.oschina.net/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://maven.oschina.net/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</profile>
复制代码

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,让更多的人能够享受到获取知识的快乐!因为本人初入职场,鉴于自身阅历有限,所以本博客内容大部分来源于网络中已有知识的汇总,欢迎各位转载,评论,大家一起学习进步!如有侵权,请及时和我联系,切实维护您的权益!
posted @   CS408  阅读(1381)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示