内网搭建maven仓库(maven配置多个远程仓库)

Maven是Java的项目配置管理工具,用来管理依赖,具体的用途就不展开说了。大部分项目,配置一个镜像仓库地址就可以了(单个mirror)。但是有的网上下载的项目需要从多个仓库查找对应的包,从网上找了...

Maven是Java的项目配置管理工具,用来管理依赖,具体的用途就不展开说了。大部分项目,配置一个镜像仓库地址就可以了(单个mirror)。但是有的网上下载的项目需要从多个仓库查找对应的包,从网上找了很多帖子,配置Maven的settings.xml文件中的多个仓库,都是从一个地方复制粘贴的,方法说的都不对。今天自己研究了一下,分享给大家。

最终的settings.xml文件配置

如果你不想看具体的配置,可以直接把我下面这个配置拿走,修改一下localRepository部分对应自己的本地repo(通常为~/.m2/repository),然后将settings.xml替换${MAVEN_HOME}/conf/settings.xml即可。

<settings xmlns=\"http://maven.apache.org/SETTINGS/1.2.0\"          xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"          xsi:schemaLocation=\"http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd\">  <localRepository>/Users/chris/.m2/repository</localRepository>  <pluginGroups></pluginGroups>  <proxies></proxies>  <servers></servers>  <mirrors></mirrors>  <profiles>    <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>pentaho</id>      <repositories>        <repository>          <id>pentaho</id>          <url>https://nexus.pentaho.org/content/repositories/omni/</url>          <releases>            <enabled>true</enabled>          </releases>          <snapshots>            <enabled>true</enabled>            <updatePolicy>always</updatePolicy>          </snapshots>        </repository>      </repositories>    </profile>    <profile>      <id>repo1</id>      <repositories>        <repository>          <id>repo1</id>          <url>https://repo1.maven.org/maven2</url>          <releases>            <enabled>true</enabled>          </releases>          <snapshots>            <enabled>true</enabled>            <updatePolicy>always</updatePolicy>          </snapshots>        </repository>      </repositories>    </profile>    <profile>      <id>repo2</id>      <repositories>        <repository>          <id>repo2</id>          <url>https://repo2.maven.org/maven2</url>          <releases>            <enabled>true</enabled>          </releases>          <snapshots>            <enabled>true</enabled>            <updatePolicy>always</updatePolicy>          </snapshots>        </repository>      </repositories>    </profile>  </profiles>  <activeProfiles>  <activeProfile>aliyun</activeProfile>  <activeProfile>pentaho</activeProfile>  <activeProfile>repo1</activeProfile>  <activeProfile>repo2</activeProfile></activeProfiles></settings>

settings.xml相关具体配置介绍

Mirrors标签部分

通常用来定义镜像仓库(repository),指定用来下载对应依赖和插件的仓库地址。使用Mirrors的理由如下:

  • 配置一个就近加速的网络仓库;
  • 配置一个你本地创建的镜像仓库;

来自Maven官方的Mirrors配置示例如下:

<settings>  ...  <mirrors>    <mirror>      <id>other-mirror</id>      <name>Other Mirror Repository</name>      <url>https://other-mirror.repo.other-company.com/maven2</url>      <mirrorOf>central</mirrorOf>    </mirror>  </mirrors>  ...</settings>

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

Profiles标签部分

settings.xml中的profile部分,包括4个二级子标签:activationrepositoriespluginRepositories and properties。profile部分的内容需要在下面进行激活。这里介绍多个镜像共同使用仅介绍Repositories部分。

Repositories

配置方式如下,指定repo地址、id:

  <profiles>    <profile>      <id>repo2</id>      <repositories>        <repository>          <id>repo2</id>          <url>https://repo2.maven.org/maven2</url>          <releases>            <enabled>true</enabled>          </releases>          <snapshots>            <enabled>true</enabled>            <updatePolicy>always</updatePolicy>          </snapshots>        </repository>      </repositories>    </profile>  </profiles>

Active Profiles标签部分

settings.xml配置文件最后的一部分就是激活公用多个仓库的重点activeProfiles。包含多个activeProfile元素,每一个activeProfile元素都用来指定一个上部分profile的id,也就是说,每指定一个activeProfile映射,就激活一个profile,将会覆盖其他任何环境配置。

大家从如下例子里,可以看到我配置了aliyun、pentaho、repo1和repo2四个仓库,这样再执行mvn命令,他就会从这四个仓库循环去查找包,第一个找不到就去第二个找。。。

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd\">  ...<activeProfiles>  <activeProfile>aliyun</activeProfile>  <activeProfile>pentaho</activeProfile>  <activeProfile>repo1</activeProfile>  <activeProfile>repo2</activeProfile></activeProfiles></settings>

阅读前请先查看【免责声明】本文内容由互联网用户自发贡献,该文观点仅代表作者本人,本站仅供展示。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 1217266901@qq.com 举报,一经查实,本站将立刻删除。 转载请注明出处:https://jingfakeji.com/tech/18646.html

上一篇 2023年09月25日 13:20
下一篇 2023年09月25日 13:20

相关推荐

  • 小飞机使用的操作系统是什么?该系统有何特点?

    在当今的科技世界中,各种应用和软件层出不穷,为我们的生活和工作带来极大便利。小飞机(可能指的是某款特定的应用程序)作为一种软件产品,其运行环境和操作系统自然也是用户关心的要点。本文将详细探讨小飞机使用的是哪种操作系统,并深入分析该系统的独特...

    2025年05月14日
    163
  • 卖音响的厂家在哪里找货源?如何联系?

    在寻找音响厂家货源时,你需要一个全面的指南来帮助你完成这项任务。不论是刚入行的新手,还是经验丰富的销售商,找到可靠的货源对于成功开展音响销售业务至关重要。以下内容将为你提供寻找音响厂家货源的详细步骤以及如何与他们建立联系,确保你能够找到合适...

    2025年05月14日
    191
  • 文峰新电源在车行业表现如何?市场评价怎么样?

    随着新能源汽车市场的快速崛起,汽车电源技术也成为了行业关注的焦点。文峰新电源作为汽车电源领域的一股新势力,其在市场上的表现和用户的评价,无疑对行业有着重要的影响。本文将深入探讨文峰新电源在汽车行业内的表现和市场评价,为行业人士及消费者提供一...

    2025年05月14日
    215
  • 手机商户如何有大量的华为?如何提高华为手机的销售量?

    在数字营销时代,提高华为手机的销售量不仅需要了解产品特性,更要深入探究市场策略和消费者心理。作为专业的SEO文章撰写者,本篇将全面解析手机商户如何大量获取华为手机,并探讨提高华为手机销售量的有效方法。无论是新手还是经验丰富的电商从业者,本文...

    2025年05月14日
    300
  • 荣耀小花手机壳如何更换?更换过程中需要注意什么?

    在当今快节奏的生活中,智能手机不仅是我们通讯的工具,更成为了我们日常生活中不可或缺的伙伴。荣耀小花系列手机因其精致的外观和良好的性能受到不少用户的喜爱。然而,随着使用时间的增长,手机壳可能会因磨损、破损而需要更换。接下来,本文将详尽介绍荣耀...

    2025年05月14日
    196
  • 振航品牌音响的性能如何?用户反馈有哪些?

    随着科技的进步和人们对生活品质追求的提高,家庭影音体验越来越受到重视。振航品牌音响作为市场上的一个新选择,其产品性能如何?用户评价又有哪些?本文将深入探讨振航音响的性能表现和用户体验,为潜在购买者提供详实的参考信息。振航品牌音响以其创新的设...

    2025年05月14日
    337

联系我们

在线咨询: QQ交谈

邮箱:1217266901@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信