1、copy 配置文件setting.xml到用户目录(非必需但强烈推荐)
idea、eclipse默认优先读取用户目录的setting.xml,所以作此配置。
从安装目录copy到 “用户/.m2”目录下
2、第一步给中央库配置镜像,拦截默认的中央库
maven默认会在默认中央仓库(仓库id为central)下载依赖。但服务器在国外,网速慢,所以配置国内maven仓库,本文以阿里云maven仓库为例。
看一下阿里云maven库,选type为group的这个public库(为什么这么选,参考/lishuoboy/article/details/99937731)
在conf/settings.xml 中配置
<mirror><id>central</id><mirrorOf>central</mirrorOf><name>阿里云</name><url>/repository/public</url></mirror>
是被拦截的仓库的id,被拦截后将从镜像库获取依赖。上面拦截maven默认的中央库central。
可以用*拦截所有仓库。模糊匹配等更高级的配置方式自己百度。
3、添加更多远程仓库(一般不需要此步骤,项目独有远程仓库的在项目中配置即可)
一个仓库不够用,因为有的依赖包不在这个仓库里,在别的仓库,比如公司自己的jar包,所以多添加几个远程仓库。
我们添加阿里云的3个代理库(其实上面的阿里云public库已经包含下面3个库的依赖,这里只是举例),在conf/settings.xml文件的标签下添加如下脚本
如果已经按照上面第1步配置了阿里云仓库,阿里云已经代理了中央仓库、jcenter、spring等仓库,下面的三个仓库已经不需要添加,此处只是举例
<profile><id>aliyun_repo</id><repositories><repository><id>jcenter</id><name>阿里云代理的jcenter库</name><url>/repository/jcenter</url><layout>default</layout><snapshotPolicy>always</snapshotPolicy></repository><repository><id>spring</id><name>阿里云代理的spring库</name><url>/repository/spring</url><layout>default</layout><snapshotPolicy>always</snapshotPolicy></repository><repository><id>spring-plugin</id><name>阿里云代理的spring-plugin库</name><url>/repository/spring-plugin</url><layout>default</layout><snapshotPolicy>always</snapshotPolicy></repository></repositories></profile>
然后在conf/settings.xml文件的标签下添加如下脚本
<activeProfiles><activeProfile>aliyun_repo</activeProfile></activeProfiles>
aliyun_repo是上面的
其他补充:
maven默认的中央仓库(central)信息
中央仓库id:central
中央仓库地址:http://repo./maven2
搜索地址: /
阿里云各种maven镜像仓库: