Maven实战_在setting.xml文件中同时配置local nexus和aliyun nexus

为了提升maven下载jar包的速度,会使用阿里云私服进行加速,在setting.xml文件的mirrors节点下,添加如下配置:

<mirror>
  <id>aliyun-nexus</id> 
  <mirrorOf>central</mirrorOf>
  <name>aliyun nexus</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

mirror可以拦截对远程仓库的请求,改变对目标仓库的下载地址,一般情况下,maven下载jar包会默认到中央仓库central进行下载,

mirrorOf可以理解为“为某个仓库(repository)的镜像”,填写的是repositoryId,当填写为 * 时,代表匹配所有的仓库,代理所有的仓库。

mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,重定向到mirror.url指定的地址。

 

公司里面的项目使用了自定义jar包,那么,就要搭建自己的nexus,将镜像配置改成如下:

<mirror>
  <id>local-nexus</id> 
  <mirrorOf>central</mirrorOf>
  <name>local nexus</name>
  <url>http://ip:port/repository/maven-public/</url>
</mirror>

因为公司内部的私服是外网部署,并且带宽很低,导致项目在使用阿里云流水线部署java构建时,下载jar包速度非常慢。

 

这个时候就想着使用阿里云私服和自己公司的私服,下载spring相关的jar包到aliyun nexus进行下载,当需要用到自定义的jar时就使用local nexus

 

我们需要用到setting.xml中的另一个配置profies,来配置自定义仓库

<profiles>
  <profile>
    <!-- 私服id -->
    <id>Nexus</id>
      <repositories>
        <repository>
          <id>aliyun-nexus</id>
          <url>https://maven.aliyun.com/repository/public</url>
            <snapshots><enabled>true</enabled></snapshots>
            <releases><enabled>true</enabled></releases>
        </repository>
       </repositories>
      
       <!--指定插件下载地址-->
       <pluginRepositories>
        <pluginRepository>
           <id>aliyun-plugin-nexus</id>
           <url>https://maven.aliyun.com/repository/public</url>
           <snapshots><enabled>true</enabled></snapshots>
           <releases><enabled>true</enabled></releases>
        </pluginRepository>
      </pluginRepositories>
  </profile>
</profiles>
  
<!--启动私服仓库 -->
<activeProfiles>
  <activeProfile>Nexus</activeProfile>
</activeProfiles>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 参考文章:

https://www.cnblogs.com/Vincent-yuan/p/14974938.html

https://www.cnblogs.com/chuimber/p/18299166

https://blog.csdn.net/weixin_43811057/article/details/132720550

 


 

posted @ 2025-04-11 14:41  喻聪  阅读(127)  评论(0)    收藏  举报