多个仓库源配置及repositories和mirrors的配置

简介: 在实际项目中会存在多个仓库,包括我们自建的Nexus私有仓库和阿里仓,这里就需要设置多仓的顺序,防止jar包不在其中一个仓库时会自动从另外一个仓库中拉取。

  在实际项目中会存在多个仓库,包括我们自建的Nexus私有仓库和阿里仓,这里就需要设置多仓的顺序,防止jar包不在其中一个仓库时会自动从另外一个仓库中拉取。

  Maven的Setting配置中有mirror和repository,它们的作用都是配置远程maven仓库的地址。repository就是直接配置站点地址,mirror则是作为站点的镜像,代理某个或某几个站点的请求,实现对repository的完全代替。

  

  有两种形式可以配置多个repository, 配置多个profile或者在同一个profile中配置多个repository.配置多profile时,还需要配置activeProfiles使配置生效。

  下载依赖时,maven会按照配置从上到下的顺序,依次尝试从各个地址下载,成功下载为止。

  无论是配置国内的maven仓库,还是配置nexus之类私服,都可以直接配置成repository, 这样即使配置的这些仓库有些问题导致一些包下不下来,也可以继续用别的仓库尝试。

 

  <repository>时<id>似乎也没什么用,如果你只是在pom.xml中配置个仓库,这个id是没什么用的,可以随便写。这个id是配合上面讲的mirror一块使用的,还记得mirrorOf吗,我们配置mirrorOf为<mirrorOf>central</mirrorOf>是,mirror中的url会将默认的central仓库的url给覆盖了,所以这里的<repository>标签下的id是给mirrorOf用的。当repository中的id与mirrorOf一致时,mirrorOf中的url就会覆盖repository中的url地址。

 

  在Mirrors部分可以配置多个镜像仓库,但是在该部分配置多个仓库,并不能提供自动查询多个仓库的功能,默认还是取第一个仓库进行查询。

  我们这里采用设置profiles的方式来达到想要的目的,直接上配置:

<?xml version="1.0" encoding="utf-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

 <localRepository>D:/m2/repository</localRepository>

 <pluginGroups>

   <pluginGroup>org.mortbay.jetty</pluginGroup>

 </pluginGroups>

 <proxies>

 </proxies>

 <servers>

   <server>

     <id>nexus-releases</id>

     <username>ali</username>

     <password>123456</password>

   </server>

 </servers>

 <mirrors>

 </mirrors>

 <profiles>

   <profile>

     <id>jdk-1.8</id>

     <activation>

       <activeByDefault>true</activeByDefault>

       <jdk>1.8</jdk>

     </activation>

     <properties>

       <maven.compiler.source>1.8</maven.compiler.source>

       <maven.compiler.target>1.8</maven.compiler.target>

       <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>

     </properties>

   </profile>

   <profile>

     <id>downloadSources</id>

     <properties>

       <downloadSources>true</downloadSources>

       <downloadJavadocs>true</downloadJavadocs>

     </properties>

   </profile>

   <profile>

     <id>aliyun</id>

     <repositories>

       <repository>

         <id>aliyun</id>

         <url>https://maven.aliyun.com/repository/public</url>

         <releases>

           <enabled>true</enabled>

         </releases>

         <snapshots>

           <enabled>true</enabled>

           <updatePolicy>always</updatePolicy>

         </snapshots>

       </repository>

     </repositories>

   </profile>

   <profile>

     <id>nexus-releases</id>

     <repositories>

       <repository>

         <id>nexus-releases</id>

         <url>http://10.3.87.5:8082/repository/maven-public/</url>

         <releases>

           <enabled>true</enabled>

         </releases>

         <snapshots>

           <enabled>true</enabled>

           <updatePolicy>always</updatePolicy>

         </snapshots>

       </repository>

     </repositories>

   </profile>

 </profiles>

 <activeProfiles>

  <activeProfile>aliyun</activeProfile>

  <activeProfile>nexus-releases</activeProfile>

  </activeProfiles>

</settings>

注意:这里面配置了maven.compiler.source,当环境中有jdk1.8以下版本时用maven打包时会报jdk低版本的提示,需要统一打包时jdk的版本,因此需要在此指定

 

目录
相关文章
|
4月前
Harbor新建仓库目标提示 the registry is unhealthy
Harbor新建仓库目标提示 the registry is unhealthy
134 0
|
5月前
|
开发工具 git
Gitlab配置mirrorRepository 镜像仓库
Gitlab配置mirrorRepository 镜像仓库 🔊业务场景📆1.在a项目中点击settings-》repository-》mirroring repositories📌tips🧣最后的话
172 0
|
Java Maven
Maven - 国内Maven仓库之阿里云Aliyun仓库地址及设置
Maven - 国内Maven仓库之阿里云Aliyun仓库地址及设置
15090 1
Maven - 国内Maven仓库之阿里云Aliyun仓库地址及设置
|
Java Maven
最快的 maven repository--阿里镜像仓库
国内速度超快的maven repository
157332 0
|
4月前
|
Java Apache Maven
maven配置阿里云镜像仓库
maven配置阿里云镜像仓库
|
8月前
|
存储 Java Maven
Maven配置仓库
当使用 Maven 构建项目时,有三种仓库起着重要作用: 1. 本地仓库:就像你自己的书库。当你使用 Maven 下载依赖项时,它们会被保存在你电脑上的一个特定目录中,以后可以重复使用。这个本地仓库存储了项目所需的库文件和插件,使得构建过程更快速,因为不需要每次都从互联网下载。 2. 中央仓库:类似于一个全球共享的图书馆。中央仓库是 Maven 社区维护的一个公共存储库,其中包含了大量的开源库文件和插件。当你在 Maven 项目中声明依赖项时,如果没有指定其他远程仓库,Maven 会自动从中央仓库下载所需的依赖项。中央仓库是 Maven 默认的远程仓库,你可以从中央仓库获取到大量常见的开源库
6307 2
|
8月前
|
Java Maven
Maven 设置仓库地址
Maven 设置仓库地址
988 0
|
运维 Docker 容器
安装Harbor仓库
安装Harbor仓库
431 0
|
Java Maven
Maven之阿里云镜像仓库配置--详解
方式一:全局配置 可以添加阿里云的镜像到maven的setting.xml配置中,这样就不需要每次在pom中,添加镜像仓库的配置,在mirrors节点下面添加子节点: nexus-a.
Maven之阿里云镜像仓库配置--详解
|
网络安全 开发工具 git
使用GitLab Mirrors同步Git仓库
笔者需实现多个Git仓库数据的同步,大致是从一个远程的Git仓库同步代码到公司内部的GitLab仓库。经过调研,笔者将目光瞄准开源的GitLab Mirrors。
10233 2