maven 多仓库和镜像设置

为什么使用镜像

当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 ...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章