maven 多仓库和镜像设置

8次阅读

共计 1316 个字符,预计需要花费 4 分钟才能阅读完成。

为什么使用镜像
当 maven 在本地找不到包的时候,就尝试从中央仓库 (https://repo1.maven.org/maven2/) 获取,有的时候我们访问外网太慢了,我们就从镜像仓库 (别的仓库或者自己的私有仓库) 获取。
设置镜像
<mirror>
<id>tz-mirror</id>
<mirrorOf>external:*,!mmkj</mirrorOf>
<name>tz test nexus repository</name>
<url>http://xxxxx:30003/repository/maven-proxy</url>
</mirror>

id 唯一标识

mirrorOf 指定镜像的规则。就是什么情况会从镜像仓库拉取,而不是从原本的仓库拉取 可选项参考链接:

* 匹配所有

external:* 除了本地缓存之后的所有仓库

repo,repo1 repo 或者 repo1。这里 repo 指的是仓库的 id,下文会提到

*,!repo1 除了 repo1 的所有仓库

name 名称描述

url 地址

上述的例子除了 mmkj 仓库之外,其他的全从自己的私有仓库获取(仓库我做了代理,会自动从中央仓库 https://repo1.maven.org/maven2/ 获取).
多仓库配置
参考链接 第三方包,自己公司的包等除了手动 install:install-file 导入之外,最好的办法就是搭建自己公司的私有仓库,这里推荐使用 nexus, 这样除了中央仓库之外就需要设置自己的仓库了
设置多仓库有 2 个方法:
pom 设置(java 的 pom 文件)
<project>

<repositories>
<repository>
<id>my-repo1</id>
<name>your custom repo</name>
<url>http://jarsm2.dyndns.dk</url>
</repository>
<repository>
<id>my-repo2</id>
<name>your custom repo</name>
<url>http://jarsm2.dyndns.dk</url>
</repository>
</repositories>

</project>
这里的 id 就是镜像 mirrorOf 使用的
setting 设置(${user.home}/.m2/settings.xml)
<settings>

<profiles>

<profile>
<id>myprofile</id>
<repositories>
<repository>
<id>my-repo2</id>
<name>your custom repo</name>
<url>http://jarsm2.dyndns.dk</url>
</repository>

</repositories>
</profile>

</profiles>

<activeProfiles>
<activeProfile>myprofile</activeProfile>
</activeProfiles>

</settings>
激活配置文件除了放在 activeProfiles 中之外,也可以使用 mvn 的参数
mvn -Pmyprofile …

正文完
 0