Maven拉取依赖总是缓慢、失败解决方案

maven 仓库配置优先级: 本地仓库(localRepositories) > profile中的repositories仓库 > POM > mirrors全局仓库

统一解决方案

  • 方式一: 修改maven镜像的方式
    第一步,修改你每个项目的 pom.xml ,在其中添加如下代码片段:
<repository>
  <!--这个 id 一定要和 maven 配置文件中的 mirrorOf 对应-->
    <id>central</id>
    <url>https://maven.aliyun.com/nexus/content/groups/public</url>
    <layout>default</layout>
    <releases>
        <enabled>true</enabled>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>

如果项目需要额外的 repository 选项,直接在下面添加即可。
第二步,修改你的maven的配置文件 settings.xml,在其中添加 mirror 选项:

<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <!--mirrorOf的配置很重要-->
    <mirrorOf>central</mirrorOf>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
  • 方式二: 修改maven软件的默认repository
<!--
可以从以下文件中找到maven仓库的默认配置如下
apache-maven-3.6.1\lib\maven-model-builder-3.6.1.jar\org\apache\maven\model\pom-4.0.0.xml
-->
<repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
</repositories>

将这段代码修改 aliyun 的即可。


本文参考于maven仓库repositories和mirrors的配置及区别

posted @ 2023-04-14 11:13  LoremMoon  阅读(742)  评论(0编辑  收藏  举报