分享

maven给默认中央仓库设置镜像为阿里云maven仓库 并添加全局远程仓库

 筱肆 2020-03-31

1、copy 配置文件setting.xml到用户目录(非必需但强烈推荐)

idea、eclipse默认优先读取用户目录的setting.xml,所以作此配置。

从安装目录copy到      “用户/.m2”目录下

2、第一步给中央库配置镜像,拦截默认的中央库

maven默认会在默认中央仓库(仓库id为central)下载依赖。但服务器在国外,网速慢,所以配置国内maven仓库,本文以阿里云maven仓库为例。

看一下阿里云maven库,选type为group的这个public库(为什么这么选,参考https://blog.csdn.net/lishuoboy/article/details/99937731

在conf/settings.xml 中配置

  1. <mirror>
  2.   <id>central</id>
  3.   <mirrorOf>central</mirrorOf>
  4.   <name>阿里云</name>
  5. <url>https://maven.aliyun.com/repository/public</url>
  6. </mirror>

<mirrorOf>是被拦截的仓库的id,被拦截后将从镜像库获取依赖。上面拦截maven默认的中央库central。

<mirrorOf>可以用*拦截所有仓库。模糊匹配等更高级的配置方式自己百度。

3、添加更多远程仓库(一般不需要此步骤,项目独有远程仓库的在项目中配置即可)

一个仓库不够用,因为有的依赖包不在这个仓库里,在别的仓库,比如公司自己的jar包,所以多添加几个远程仓库。

我们添加阿里云的3个代理库(其实上面的阿里云public库已经包含下面3个库的依赖,这里只是举例),在conf/settings.xml文件的<profiles></profiles>标签下添加如下脚本

如果已经按照上面第1步配置了阿里云仓库,阿里云已经代理了中央仓库、jcenter、spring等仓库,下面的三个仓库已经不需要添加,此处只是举例

  1. <profile>
  2. <id>aliyun_repo</id>
  3. <repositories>
  4. <repository>
  5. <id>jcenter</id>
  6. <name>阿里云代理的jcenter库</name>
  7. <url>https://maven.aliyun.com/repository/jcenter</url>
  8. <layout>default</layout>
  9. <snapshotPolicy>always</snapshotPolicy>
  10. </repository>
  11. <repository>
  12. <id>spring</id>
  13. <name>阿里云代理的spring库</name>
  14. <url>https://maven.aliyun.com/repository/spring</url>
  15. <layout>default</layout>
  16. <snapshotPolicy>always</snapshotPolicy>
  17. </repository>
  18. <repository>
  19. <id>spring-plugin</id>
  20. <name>阿里云代理的spring-plugin库</name>
  21. <url>https://maven.aliyun.com/repository/spring-plugin</url>
  22. <layout>default</layout>
  23. <snapshotPolicy>always</snapshotPolicy>
  24. </repository>
  25. </repositories>
  26. </profile>

然后在conf/settings.xml文件的<settings></settings>标签下添加如下脚本

  1. <activeProfiles>
  2. <activeProfile>aliyun_repo</activeProfile>
  3. </activeProfiles>

如下图

aliyun_repo是上面<profile>的<id>

其他补充:

maven默认的中央仓库(central)信息

中央仓库id:central

中央仓库地址:http://repo.maven./maven2

搜索地址:   https://search./


阿里云各种maven镜像仓库:  http://maven.aliyun.com

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多