maven配置阿里云、腾讯云镜像mirror代理仓库,添加多个global全局远程仓库
目录
1. 准备工作
1.1 安装maven
下载maven压缩包,解压到C:\ProgramMe,然后配置环境变量(参考https://www.runoob.com/maven/maven-setup.html)
1.2 copy配置文件setting.xml(可选)
idea、eclipse默认优先读取用户目录的setting.xml,所以作此配置。
构建配置文件的类型:https://www.runoob.com/maven/maven-build-profiles.html
将 %M2_HOME%/conf/settings.xml 复制到 %USER_HOME%/.m2/settings.xml
即将 C:\ProgramMe/apache-maven-3.6.3/conf/settings.xml 复制到 C:/Users\lishuoboy/.m2/settings.xml
2. 几个仓库的信息
maven默认的中央仓库(central)信息
中央仓库id:central
中央仓库地址:http://repo.maven.apache.org/maven2 或者 https://repo1.maven.org/maven2/
搜索地址: https://mvnrepository.com/ 或 https://search.maven.org/
阿里云maven仓库官网:https://maven.aliyun.com/
腾讯云maven仓库官网:https://mirrors.tencent.com/
打开网址看一下阿里云maven库,选type为group的这个public库作为中央仓库的mirror(public库聚合了central 和 jcenter 两个库)
腾讯官网查看maven地址
点击查看后
3. 配置中央库的镜像mirror仓库
3.1 配置目的
默认情况下,maven有一个远程仓库,就是中央仓库,如下图。所有依赖从中央仓库下载。但服务器在国外,网速慢,所以用国内镜像仓库替换。
3.2 配置方法
在 setting.xml 文件的 <mirrors></mirrors> 标签下添加以下代码。
<mirror>
<id>aliyun</id>
<mirrorOf>central,jcenter</mirrorOf>
<name>阿里云公共库,聚合了central和jcenter,包括release和snapshot</name>
<url>https://maven.aliyun.com/repository/public/</url>
</mirror>
注意:
① <mirrorof>不要写*,url代理了哪个写那个。写*会导致任何远程仓库都被拦截,如果依赖不在这个mirror仓库,就找不到了。具体见依赖查找顺序: https://blog.csdn.net/lishuoboy/article/details/119887006?spm=1001.2014.3001.5501
②<mirror>只能在setting.xml中配置,不能pom.xml配置。可以换一种思路,即第“5. 添加更多全局远程仓库”步骤方式配置<repositories>和<pluginRepositories>,将id设置为central即可。
4. 添加deploy私库,并将私库设置为远程仓库
4.1 配置目的
公司内部不同项目之间需要相互引用,无法发布到中央库,所以需要发不到私库,给其它项目使用。同时deploy到私库的依赖需要引用下载,需要配置为远程仓库
4.2 配置方法
① 在<servers></servers>标签中添加如下脚本
<!-- 阿里云 deploy 私库 releases -->
<server>
<id>aliyun-rdc-releases</id>
<username>你的用户名</username>
<password>你的密码</password>
</server>
<!-- 阿里云 deploy 私库 snapshots -->
<server>
<id>aliyun-rdc-snapshots</id>
<username>你的用户名</username>
<password>你的密码</password>
</server>
② 在setting.xml 文件的<profiles></profiles>标签中添加如下脚本
<profile>
<!-- lishuoboy 个人 -->
<id>lishuoboy-dev</id>
<properties>
<!-- 阿里云 deploy 私库 releases -->
<altReleaseDeploymentRepository>
aliyun-rdc-releases::default::https://packages.aliyun.com/maven/repository/2022495-release-MjBYbN/
</altReleaseDeploymentRepository>
<!-- 阿里云 deploy 私库 snapshots -->
<altSnapshotDeploymentRepository>
aliyun-rdc-snapshots::default::https://packages.aliyun.com/maven/repository/2022495-snapshot-KPhWtM/
</altSnapshotDeploymentRepository>
</properties>
<!-- 可以在这里设置激活,也可以在下面<activeProfiles>中激活 -->
<!--
<activation>
<activeByDefault>true</activeByDefault>
</activation>
-->
</profile>
或者在项目pom.xml文件的<properties>中添加
<properties>
<!-- 阿里云 deploy 私库 releases -->
<altReleaseDeploymentRepository>
aliyun-rdc-releases::default::https://packages.aliyun.com/maven/repository/2022495-release-MjBYbN/
</altReleaseDeploymentRepository>
<!-- 阿里云 deploy 私库 snapshots -->
<altSnapshotDeploymentRepository>
aliyun-rdc-snapshots::default::https://packages.aliyun.com/maven/repository/2022495-snapshot-KPhWtM/
</altSnapshotDeploymentRepository>
</properties>
或者在项目pom.xml文件中添加<distributionManagement>
<distributionManagement>
<!-- 阿里云 deploy 私库 releases -->
<repository>
<id>aliyun-rdc-releases</id>
<name>阿里云 deploy 私库 releases</name>
<url>https://packages.aliyun.com/maven/repository/2022495-release-MjBYbN/</url>
</repository>
<!-- 阿里云 deploy 私库 snapshots -->
<snapshotRepository>
<id>aliyun-rdc-snapshots</id>
<name>阿里云 deploy 私库 snapshots</name>
<url>https://packages.aliyun.com/maven/repository/2022495-snapshot-KPhWtM/</url>
</snapshotRepository>
</distributionManagement>
③在setting.xml 文件的<activeProfiles></activeProfiles>标签中添加如下脚本。(在项目的pom.xml配置deploy私库的不需要此步骤)
<activeProfiles>
<!-- 设置默认激活。激活多个,只有最后一个生效 -->
<activeProfile>lishuoboy-dev</activeProfile>
</activeProfiles>
也可以在idea中配置激活/不激活对应的profile
至此,在idea中双击deploy (即执行mvn deploy命令)发布依赖了。
④在对应的<profile>中添加<repositories>和<pluginRepositories>
到第③步已经可以deploy依赖了,但是还是无法下载,所以继续配置普通仓库和插件仓库
<repositories>
<repository>
<id>aliyun-rdc-releases</id>
<name>阿里云私库 releases</name>
<url>https://packages.aliyun.com/maven/repository/2022495-release-MjBYbN/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>aliyun-rdc-snapshots</id>
<name>阿里云私库 snapshots</name>
<url>https://packages.aliyun.com/maven/repository/2022495-snapshot-KPhWtM/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-rdc-releases</id>
<name>阿里云私库 releases</name>
<url>https://packages.aliyun.com/maven/repository/2022495-release-MjBYbN/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>aliyun-rdc-snapshots</id>
<name>阿里云私库 snapshots</name>
<url>https://packages.aliyun.com/maven/repository/2022495-snapshot-KPhWtM/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
5. 添加更多全局远程仓库
上面几步添加了阿里的私库为远程仓库,有的依赖没有在阿里云仓库或者阿里云仓库挂了咋办,所以添加更多仓库。我们以阿里云和腾讯云库为例。
继续在对应的<profile>中添加<repositories>和<pluginRepositories>
<repositories>
<repository>
<id>aliyun-public</id>
<name>阿里云公共库。聚合了central和jcenter,包括release和snapshot</name>
<url>https://maven.aliyun.com/repository/public/</url>
</repository>
<repository>
<id>tencent-public</id>
<name>腾讯云公共库。包括release和snapshot</name>
<url>https://mirrors.tencent.com/nexus/repository/maven-public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-public</id>
<name>阿里云公共库,聚合了central和jcenter,包括release和snapshot</name>
<url>https://maven.aliyun.com/repository/public/</url>
</pluginRepository>
<pluginRepository>
<id>tencent-public</id>
<name>腾讯云公共库。包括release和snapshot</name>
<url>https://mirrors.tencent.com/nexus/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
注意:其实“阿里云私库”已经聚合了“aliyun-public”库的依赖
6. 完整的配置setting.xml
6.1 附件见qq邮箱
6.2 xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<!-- 阿里云 deploy 私库 releases -->
<server>
<id>aliyun-rdc-releases</id>
<username>你的用户名</username>
<password>你的密码</password>
</server>
<!-- 阿里云 deploy 私库 snapshots -->
<server>
<id>aliyun-rdc-snapshots</id>
<username>你的用户名</username>
<password>你的密码</password>
</server>
</servers>
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>central,jcenter</mirrorOf>
<name>阿里云公共库,聚合了central和jcenter,包括release和snapshot</name>
<url>https://maven.aliyun.com/repository/public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<!-- lishuoboy 个人 -->
<id>lishuoboy-dev</id>
<properties>
<!-- 阿里云 deploy 私库 releases -->
<altReleaseDeploymentRepository>
aliyun-rdc-releases::default::https://packages.aliyun.com/maven/repository/2022495-release-MjBYbN/
</altReleaseDeploymentRepository>
<!-- 阿里云 deploy 私库 snapshots -->
<altSnapshotDeploymentRepository>
aliyun-rdc-snapshots::default::https://packages.aliyun.com/maven/repository/2022495-snapshot-KPhWtM/
</altSnapshotDeploymentRepository>
</properties>
<repositories>
<repository>
<id>aliyun-rdc-releases</id>
<name>阿里云私库 releases</name>
<url>https://packages.aliyun.com/maven/repository/2022495-release-MjBYbN/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>aliyun-rdc-snapshots</id>
<name>阿里云私库 snapshots</name>
<url>https://packages.aliyun.com/maven/repository/2022495-snapshot-KPhWtM/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>aliyun-public</id>
<name>阿里云公共库。聚合了central和jcenter,包括release和snapshot</name>
<url>https://maven.aliyun.com/repository/public/</url>
</repository>
<repository>
<id>tencent-public</id>
<name>腾讯云公共库。包括release和snapshot</name>
<url>https://mirrors.tencent.com/nexus/repository/maven-public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-rdc-releases</id>
<name>阿里云私库 releases</name>
<url>https://packages.aliyun.com/maven/repository/2022495-release-MjBYbN/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>aliyun-rdc-snapshots</id>
<name>阿里云私库 snapshots</name>
<url>https://packages.aliyun.com/maven/repository/2022495-snapshot-KPhWtM/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>aliyun-public</id>
<name>阿里云公共库,聚合了central和jcenter,包括release和snapshot</name>
<url>https://maven.aliyun.com/repository/public/</url>
</pluginRepository>
<pluginRepository>
<id>tencent-public</id>
<name>腾讯云公共库。包括release和snapshot</name>
<url>https://mirrors.tencent.com/nexus/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
<!-- 可以在这里设置激活,也可以在下面<activeProfiles>中激活 -->
<!--
<activation>
<activeByDefault>true</activeByDefault>
</activation>
-->
</profile>
</profiles>
<activeProfiles>
<!-- 设置默认激活。激活多个,只有最后一个生效 -->
<activeProfile>lishuoboy-dev</activeProfile>
</activeProfiles>
</settings>