首页 > 解决方案 > 如何强制 Maven 使用特定的存储库(仅此而已)?

问题描述

我正在设置一个本地 Nexus 存储库,并试图说服我的本地 Maven 设置以唯一的方式访问该存储库。那个 Nexus 存储库有一个组 maven-central 定义(引用 Nexus 手册:)“......访问中央存储库,以前称为 Maven 中心”。并且还应该缓存下载的数据。

因此,我将本地 Nexus 存储库输入到我的 [home]\.m2\settings.xml 中,如下所示:

    ...
    <profile>
        <id>alwaysActiveProfile</id>
        <repositories>
            <repository>
                <id>RepoSynoProxy</id>
                <name>Nexus Repo on Synology NAS</name>
                <url>http://192.168.1.15:8081/repository/maven-central/</url>
            </repository>
        </repositories>
    </profile>
  </profiles>
  ...
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
  </activeProfiles>
   ...

但出于某种奇怪的原因,当我运行 Maven 时,它开始从https://repo.maven.apache.org/maven2/下载所有内容,而不是从本地 URL 下载,正如我所料:

...
[INFO] Downloading from : http://192.168.1.15:8081/repository/maven-central/junit/junit/3.8.1/junit-3.8.1.pom
[INFO] Downloading from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom (998 B at 7.3 kB/s)
[INFO] Downloading from : http://192.168.1.15:8081/repository/maven-central/junit/junit/3.8.1/junit-3.8.1.jar
[INFO] Downloading from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar (121 kB at 1.4 MB/s)
...

我的问题是:

首先:任何人都知道,为什么对http://192.168.1.15:8081/repository/maven-central/的请求......显然不起作用?我在这里使用的是新下载并安装的 Nexus v3.18.0。Maven-central 代理已经预定义,我没有特别配置。显然,这不是“开箱即用”的。

第二: Maven 在哪里有 URL https://repo.maven.apache.org/maven2/ ... 从它用作替代方案?如果回购不起作用,是否将其硬编码到 Maven 中作为后备?可以抑制它,使其仅访问 settings.xml 中定义的存储库吗?

标签: mavennexus

解决方案


您需要在 settings.xml 中配置一个镜像,它将覆盖 superpom 和其他 pom 文件中的存储库定义: https ://help.sonatype.com/display/NXRM3/Maven+Repositories#MavenRepositories-ConfiguringApacheMaven


推荐阅读