maven 多仓库和镜像设置

为何使用镜像

当maven在本地找不到包的时候,就尝试从中央仓库(https://repo1.maven.org/maven2/)获取,有的时候咱们访问外网太慢了,咱们就从镜像仓库(别的仓库或者本身的私有仓库)获取。html

设置镜像

<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 指定镜像的规则。就是什么状况会从镜像仓库拉取,而不是从本来的仓库拉取
    可选项参考连接:java

    1. * 匹配全部
    2. external:* 除了本地缓存以后的全部仓库
    3. repo,repo1 repo 或者 repo1。 这里repo指的是仓库的id,下文会提到
    4. *,!repo1 除了repo1的全部仓库
  • name 名称描述
  • url 地址

上述的例子
除了mmkj仓库以外,其余的全从本身的私有仓库获取(仓库我作了代理,会自动从中央仓库https://repo1.maven.org/maven2/获取).apache

多仓库配置

参考连接
第三方包,本身公司的包等除了手动install:install-file导入以外,最好的办法就是搭建本身公司的私有仓库,这里推荐使用nexus, 这样除了中央仓库以外就须要设置本身的仓库了缓存

设置多仓库有2个方法:maven

  • 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 ...
相关文章
相关标签/搜索