蓝军切尔西 發表於 2026-1-11 10:23:04

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

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>场景:</li><li>引申:</li></ul></div><p>最近在工作中,拉取一个jar包,不确定最初在maven的setting.xml配置的镜像配置能否拉取到,根据AI搜索资料得到一些启发。</p>
<p class="maodian"></p><h2>场景:</h2>
<p>我在我的maven setting.xml配置了阿里云的镜像,这是我们国内开发大多数的必要配置,原因是通过阿里云的拉取,可以加快拉取速度:</p>
<div class="jb51code"><pre class="brush:xml;">        &lt;mirror&gt;
                &lt;id&gt;nexus-aliyun&lt;/id&gt;
                &lt;mirrorOf&gt;central&lt;/mirrorOf&gt;
                &lt;name&gt;Nexus aliyun&lt;/name&gt;
                &lt;url&gt;https://maven.aliyun.com/repository/central&lt;/url&gt;
       &lt;/mirror&gt;
</pre></div>
<p>在我项目的pom.xml需要拉取一些jar,发现拉取过程中提示拉取不到jar包,于是,我根据搜索到的资料再pom.xml加了仓库配置:</p>
<div class="jb51code"><pre class="brush:xml;">&lt;repositories&gt;
    &lt;repository&gt;
      &lt;id&gt;central-ma&lt;/id&gt;
      &lt;url&gt;https://repo1.maven.org/maven2/&lt;/url&gt;
      &lt;releases&gt;&lt;enabled&gt;true&lt;/enabled&gt;&lt;/releases&gt;
      &lt;snapshots&gt;&lt;enabled&gt;false&lt;/enabled&gt;&lt;/snapshots&gt;
    &lt;/repository&gt;

    &lt;repository&gt;
      &lt;id&gt;eclipse-releases&lt;/id&gt;
      &lt;url&gt;https://repo.eclipse.org/content/repositories/releases/&lt;/url&gt;
    &lt;/repository&gt;

&lt;/repositories&gt;
</pre></div>
<p>然后maven的setting.xml配置改成:</p>
<div class="jb51code"><pre class="brush:xml;">        &lt;mirror&gt;
                &lt;id&gt;nexus-aliyun&lt;/id&gt;
                &lt;mirrorOf&gt;central,!central-ma&lt;/mirrorOf&gt;
                &lt;name&gt;Nexus aliyun&lt;/name&gt;
                &lt;url&gt;https://maven.aliyun.com/repository/central&lt;/url&gt;
       &lt;/mirror&gt;
</pre></div>
<p>这样一修改,理解上有所改变,接下来,对新配置点理解进行分析</p>
<div class="jb51code"><pre class="brush:xml;">        &lt;mirror&gt;
                &lt;id&gt;nexus-aliyun&lt;/id&gt;
                &lt;mirrorOf&gt;central,!central-ma&lt;/mirrorOf&gt;
                &lt;name&gt;Nexus aliyun&lt;/name&gt;
                &lt;url&gt;https://maven.aliyun.com/repository/central&lt;/url&gt;
       &lt;/mirror&gt;
</pre></div>
<p>-<code> mirror</code> 定义了一个镜像(mirror),Maven 会把对某些仓库的请求重定向到你指定的镜像地址,从而加速下载(尤其是国内访问官方中央仓库很慢的情况下)。</p>
<ul><li>&lt;id&gt;nexus-aliyun&lt;/id&gt;:这个镜像的唯一标识符,建议唯一,通常和仓库的 id 相关联。</li><li>&lt;url&gt;https://maven.aliyun.com/repository/central&lt;/url&gt;:真正的镜像地址,所有被镜像的请求都会重定向到这里。这是阿里云提供的 Maven 中央仓库公共镜像,速度很快。</li><li>&lt;mirrorOf&gt;central,!central-ma&lt;/mirrorOf&gt;:这是最关键的部分,表示这个镜像代理哪些仓库。拆解一下:central:指的是 Maven 默认的中央仓库,其 id 在 super pom 中定义为 &quot;central&quot;,地址是repo.maven.apache.org/maven2。</li><li>!central-ma:这里的 ! 表示排除(取反)。所以整体意思是:镜像所有 id 为 &quot;central&quot; 的仓库请求,把它们重定向到阿里云镜像。但排除 id 为 &quot;central-ma&quot; 的仓库,这个仓库的请求不会走阿里云镜像,而是走它自己原本配置的地址。</li></ul>
<p class="maodian"></p><h2>引申:</h2>
<p>我们要明白的是,maven所有的项目都隐式继承Super POM,这是maven内置的默认配置, 在Super POM,中默认定义了一个仓库:</p>
<div class="jb51code"><pre class="brush:xml;">&lt;repository&gt;
&lt;id&gt;central&lt;/id&gt;
&lt;name&gt;Central Repository&lt;/name&gt;
&lt;url&gt;https://repo.maven.apache.org/maven2&lt;/url&gt;
&lt;/repository&gt;
</pre></div>
<p>这是Maven Central Repository(Maven 中央仓库),全球最大的公共开源 jar 包仓库。 所以这里的central就关联上上面setting.xml mirrof的central。</p>
<p>当你的 <strong>pom.xml</strong> 中只声明 而<strong>没有额外配置 </strong> 时,Maven 会:</p>
<ol><li>先检查本地仓库(~/.m2/repository)。</li><li>如果本地没有,就去默认的 &quot;central&quot; 仓库下载。</li></ol>
<p>具体在实际运用中,pom.xml的repositories根据使用者自由配置,可以是公司内网仓库,可以是其他仓库,我在这里是以maven的 repo1.maven.org/maven2 来配置repository举例。</p>
<p>到此这篇关于maven的多仓库配置的实现步骤的文章就介绍到这了,更多相关maven 多仓库配置内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>Maven多仓库多镜像源配置过程</li><li>maven&nbsp;setting多仓库配置方式</li><li>Maven配置单仓库与多仓库的实现(Nexus)</li><li>Maven配置多仓库无效的解决</li><li>详解maven配置多仓库的方法示例</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: maven的多仓库配置的实现步骤