Java Maven settings.xml中私有仓库配置详解

作者:BJT 时间:2022-02-19 15:36:50 

Maven setting中私有仓库配置浅析

最近遇到过不少这样那样的问题,曾经做过maven的分享,但是发现当时部分内容还是太想当然了,下面经过尝试后简单总结下:

首先几个逻辑:

  • pom>启用的profile>maven原有配置

  • mirror配置mirrorOf和id匹配优先

简单maven配置

一般大家的配置(略去无关私有仓库配置)都是这样的


<mirrors>
 <mirror>
     <id>nexus</id>
     <name>mvn.xxx.com</name>
     <mirrorOf>central</mirrorOf>
     <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>        
 </mirror>
</mirrors>
 <profile>
     <id>dev</id>
     <repositories>      
     <repository>
       <id>nexus</id>
       <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
       <releases><enabled>true</enabled></releases>
                 <snapshots><enabled>true</enabled></snapshots>
     </repository>

<repository>
             <id>alibaba</id>
             <url>http://code.alibabatech.com/mvn/releases/</url>
             <releases>
                 <enabled>true</enabled>
             </releases>
             <snapshots>
                 <enabled>false</enabled>
             </snapshots>
         </repository>

</repositories>

<pluginRepositories>
         <pluginRepository>
       <id>nexus</id>
       <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
       <releases><enabled>true</enabled></releases>
                 <snapshots><enabled>true</enabled></snapshots>
     </pluginRepository>
     </pluginRepositories>
 </profile>  
<activeProfiles>
 <activeProfile>dev</activeProfile>
</activeProfiles>

mirrors

这个标签重要的属性包括id、mirrorOf。id用来唯一区分。mirrorOf用来关联repository。
url用来表示 * 地址。

mirrorOf常见大家配置成*、central、repo啥的。这里刚才提到了是用来关联respository的,等提到下面<respository>标签的时候在解释。

profile

这个就简单说下吧,就是算是个配置,可以配多个,具体哪个生效可以通过mvn命令指定,或者配置<activeProfiles>

repositories

这里面算是配置的重点


<repository>
     <id>alibaba</id>
     <url>http://code.alibabatech.com/mvn/releases/</url>
     <releases>
             <enabled>true</enabled>
     </releases>
     <snapshots>
              <enabled>false</enabled>
     </snapshots>
</repository>

几个重要的配置,一目了然吧,id标识,url地址,是否从该仓库下release,是否从该仓库下快照版本。
这里就有人会懵逼了,这里怎么又配了个地址,跟mirrors里面的地址哪个生效呢?

好的,那咱们试试。先规定一下配置:


<mirrors>
   <mirror>
       <id>nexus</id>
       <name>mvn.ws.netease.com</name>
       <mirrorOf>central</mirrorOf>
       <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>      
   </mirror>
 </mirrors>
 <repositories>      
       <repository>
         <id>nexus</id>
         <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
         <releases><enabled>true</enabled></releases>
         <snapshots><enabled>true</enabled></snapshots>
       </repository>
</repositories>

把地址区分下,mirror里配成xxx,repository配成ccc
随便找一个项目,设定一个不存在的依赖,mvn -U compile下:

Java Maven settings.xml中私有仓库配置详解

可以发现去ccc找了。说明repository里的生效了。

那么mirror里的地址什么时候生效呢?其实刚才说了,mirror里的是靠mirrorOf中的内容和repository中id关联的。比如我们把刚才配置改为


<mirrors>
   <mirror>
       <id>nexus</id>
       <name>mvn.ws.netease.com</name>
           <mirrorOf>central</mirrorOf>
       <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>      
   </mirror>
 </mirrors>
 <repositories>      
       <repository>
         <id>central</id>
         <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
         <releases><enabled>true</enabled></releases>
                   <snapshots><enabled>true</enabled></snapshots>
       </repository>
</repositories>

把repository中的id改成central

Java Maven settings.xml中私有仓库配置详解

这样就行了。此外mirrorOf中可以配置通配符,例如*,表示任何repository都和这个关联。

其实简单来说就是如果repository的id能和mirrorOf关联上,那么url以mirror的为准,否则以repository中自己的url为准。

其他还有一些点,repositories中可以配置多个repository,配置多个话,一个找不到会找下一个,比如我们在刚才基础上加上阿里的配置


<repositories>      
       <repository>
         <id>nexus</id>
         <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
         <releases><enabled>true</enabled></releases>
         <snapshots><enabled>true</enabled></snapshots>
       </repository>
       <repository>
               <id>alibaba</id>
               <url>http://code.alibabatech.com/mvn/releases/</url>
               <releases>
                   <enabled>true</enabled>
               </releases>
               <snapshots>
                   <enabled>false</enabled>
               </snapshots>
           </repository>
</repositories>

在构建一次:

Java Maven settings.xml中私有仓库配置详解

当配置多个时,会逐一进行下载尝试。

来源:https://www.jianshu.com/p/b734f075a85a

标签:Maven,settings.xml
0
投稿

猜你喜欢

  • Java多线程之彻底搞懂线程池

    2023-12-18 23:54:09
  • java实现幸运抽奖功能

    2023-11-27 07:24:16
  • Swagger注解-@ApiModel和@ApiModelProperty的用法

    2023-02-05 23:57:48
  • Java面向对象程序设计:类的定义,静态变量,成员变量,构造函数,封装与私有,this概念与用法详解

    2022-07-12 03:35:14
  • Springcloud Nacos基本操作代码实例

    2023-07-07 18:24:22
  • Java设计模式之工厂模式案例详解

    2023-11-27 20:08:03
  • Spring使用三级缓存解决循环依赖的问题

    2023-03-14 09:06:15
  • Java使用HttpUtils实现发送HTTP请求

    2021-06-11 07:08:39
  • Java 使用 HttpClient 发送 GET请求和 POST请求

    2023-07-23 07:56:13
  • 在Eclipse中运行Solr 基础知识

    2021-07-06 22:51:04
  • Java8中LocalDateTime与时间戳timestamp的互相转换

    2023-11-10 05:20:21
  • spring中bean的生命周期详解

    2021-11-29 23:31:02
  • springcloud注册hostname或者ip的那些事

    2022-05-06 00:57:37
  • 详解如何在Spring Security中自定义权限表达式

    2023-11-08 05:11:24
  • SpringBoot整合Shiro的代码详解

    2023-10-30 10:53:31
  • Java 格式化输出JSON字符串的2种实现操作

    2023-11-13 09:41:10
  • Springboot2.0处理自定义异常并返回json

    2021-06-01 03:42:24
  • Android利用Flutter实现立体旋转效果

    2023-06-20 08:20:32
  • 浅谈Java消息队列总结篇(ActiveMQ、RabbitMQ、ZeroMQ、Kafka)

    2022-06-13 01:30:40
  • 使用springCloud+nacos集成seata1.3.0搭建过程

    2022-06-19 02:48:47
  • asp之家 软件编程 m.aspxhome.com