当前位置: 代码网 > it编程>编程语言>Java > maven的多仓库配置的实现步骤

maven的多仓库配置的实现步骤

2026年01月11日 Java 我要评论
最近在工作中,拉取一个jar包,不确定最初在maven的setting.xml配置的镜像配置能否拉取到,根据ai搜索资料得到一些启发。场景:我在我的maven setting.xml配置了阿里云的镜像

最近在工作中,拉取一个jar包,不确定最初在maven的setting.xml配置的镜像配置能否拉取到,根据ai搜索资料得到一些启发。

场景:

我在我的maven setting.xml配置了阿里云的镜像,这是我们国内开发大多数的必要配置,原因是通过阿里云的拉取,可以加快拉取速度:

	<mirror>
  		<id>nexus-aliyun</id>
  		<mirrorof>central</mirrorof>
  		<name>nexus aliyun</name>
  		<url>https://maven.aliyun.com/repository/central</url>
	 </mirror>  

在我项目的pom.xml需要拉取一些jar,发现拉取过程中提示拉取不到jar包,于是,我根据搜索到的资料再pom.xml加了仓库配置:

<repositories>
    <repository>
        <id>central-ma</id>
        <url>https://repo1.maven.org/maven2/</url>
        <releases><enabled>true</enabled></releases>
        <snapshots><enabled>false</enabled></snapshots>
    </repository>

    <repository>
        <id>eclipse-releases</id>
        <url>https://repo.eclipse.org/content/repositories/releases/</url>
    </repository>

</repositories>

然后maven的setting.xml配置改成:

	<mirror>
  		<id>nexus-aliyun</id>
  		<mirrorof>central,!central-ma</mirrorof>
  		<name>nexus aliyun</name>
  		<url>https://maven.aliyun.com/repository/central</url>
	 </mirror>  

这样一修改,理解上有所改变,接下来,对新配置点理解进行分析

	<mirror>
  		<id>nexus-aliyun</id>
  		<mirrorof>central,!central-ma</mirrorof>
  		<name>nexus aliyun</name>
  		<url>https://maven.aliyun.com/repository/central</url>
	 </mirror>  

- mirror 定义了一个镜像(mirror),maven 会把对某些仓库的请求重定向到你指定的镜像地址,从而加速下载(尤其是国内访问官方中央仓库很慢的情况下)。

  • <id>nexus-aliyun</id>:这个镜像的唯一标识符,建议唯一,通常和仓库的 id 相关联。
  • <url>https://maven.aliyun.com/repository/central</url>:真正的镜像地址,所有被镜像的请求都会重定向到这里。这是阿里云提供的 maven 中央仓库公共镜像,速度很快。
  • <mirrorof>central,!central-ma</mirrorof>:这是最关键的部分,表示这个镜像代理哪些仓库。拆解一下:central:指的是 maven 默认的中央仓库,其 id 在 super pom 中定义为 "central",地址是repo.maven.apache.org/maven2。
  • !central-ma:这里的 ! 表示排除(取反)。所以整体意思是:镜像所有 id 为 "central" 的仓库请求,把它们重定向到阿里云镜像。但排除 id 为 "central-ma" 的仓库,这个仓库的请求不会走阿里云镜像,而是走它自己原本配置的地址。

引申:

我们要明白的是,maven所有的项目都隐式继承super pom,这是maven内置的默认配置, 在super pom,中默认定义了一个仓库:

<repository>
  <id>central</id>
  <name>central repository</name>
  <url>https://repo.maven.apache.org/maven2</url>
</repository>

这是maven central repository(maven 中央仓库),全球最大的公共开源 jar 包仓库。 所以这里的central就关联上上面setting.xml mirrof的central。

当你的 pom.xml 中只声明 而没有额外配置 时,maven 会:

  1. 先检查本地仓库(~/.m2/repository)。
  2. 如果本地没有,就去默认的 "central" 仓库下载。

具体在实际运用中,pom.xml的repositories根据使用者自由配置,可以是公司内网仓库,可以是其他仓库,我在这里是以maven的 repo1.maven.org/maven2 来配置repository举例。

到此这篇关于maven的多仓库配置的实现步骤的文章就介绍到这了,更多相关maven 多仓库配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com