在使用maven构建中,使用仓库镜像,可以加速构建,这里介绍仓库镜像使用方法,留作笔记。
配置说明
使用仓库镜像
配置一个仓库的镜像,可以编辑配置文件(${user.home}/.m2/settings.xml):
如果没有${user.home}/.m2/settings.xml,可以从maven安装目录的conf目录下复制。
<settings>
...
<mirrors>
<mirror>
<id>other-mirror</id>
<name>other mirror repository</name>
<url>https://other-mirror.repo.other-company.com/maven2</url>
<mirrorof>central</mirrorof>
</mirror>
</mirrors>
...
</settings>
参数说明:
| 参数 | 说明 |
|---|---|
| id | 镜像id |
| name | 镜像名 |
| url | 镜像url |
| mirrorof | 镜像仓库替换的目标仓库 |
使用单一仓库
如果希望所有maven请求都访问一个仓库(比如自建仓库),可以指定maven使用单一仓库,配置如下:
<settings>
...
<mirrors>
<mirror>
<id>internal-repository</id>
<name>maven repository manager running on repo.mycompany.com</name>
<url>http://repo.mycompany.com/proxy</url>
<mirrorof>*</mirrorof>
</mirror>
</mirrors>
...
</settings>
高级配置
mirrorof还支持更高级的语法,示例如下:
| 值 | 作用 |
|---|---|
| * | 匹配所有仓库 |
| external:* | 匹配所有仓库,除了localhost仓库和基于文件的仓库 |
| repo,repo1 | 匹配repo,repo1两个仓库 |
| *,!repo1 | 匹配所有仓库,处理repo1 |
常见用法
假设项目的pom中的仓库配置如下:
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>cloudera.repo</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
<name>cloudera repo</name>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>hortonworks.repo</id>
<url>https://repo.hortonworks.com/content/repositories/releases</url>
<name>hortonworks repo</name>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
用法一
使用阿里云maven镜像替换central仓库,其他不变,配置如下:
<settings>
...
<mirrors>
<mirror>
<id>aliyunmaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorof>central</mirrorof>
</mirror>
</mirrors>
...
</settings>
用法二
除了cloudera.repo和hortonworks.repo,都使用阿里云镜像,配置如下:
<settings>
...
<mirrors>
<mirror>
<id>aliyunmaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorof>*,!cloudera.repo,!hortonworks.repo</mirrorof>
</mirror>
</mirrors>
...
</settings>
参考:https://maven.apache.org/guides/mini/guide-mirror-settings.html
到此这篇关于maven仓库镜像配置的方法实现的文章就介绍到这了,更多相关maven仓库镜像配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论