Maven 配置学习:存在两个本地私服如何配置

Maven 配置学习:存在两个本地私服如何配置

目录

  • [Maven 配置学习:存在两个本地私服如何配置](#Maven 配置学习:存在两个本地私服如何配置)

如果需要配置两个私服和一个公共仓库,并使 IntelliJ IDEA 可以按照指定顺序尝试下载依赖,可以在 Maven 的 settings.xml 文件中进行配置。下面是一个包含两个私有仓库和一个公共仓库(阿里云公共仓库)的配置示例:

xml 复制代码
<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">

  <!-- 本地仓库位置 -->
  <localRepository>D:/m2_repo</localRepository>

  <profiles>
    <profile>
      <id>custom-repositories</id>
      <repositories>
        <!-- 私有仓库 1 -->
        <repository>
          <id>private-repo-1</id>
          <url>http://192.168.0.203:8000/nexus/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>

        <!-- 私有仓库 2 -->
        <repository>
          <id>private-repo-2</id>
          <url>http://192.168.0.66:8089/nexus/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>

        <!-- 阿里云公共仓库 -->
        <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>
  </profiles>

  <activeProfiles>
    <!-- 启用配置的自定义仓库 -->
    <activeProfile>custom-repositories</activeProfile>
  </activeProfiles>
</settings>

解释:

1.本地仓库位置:

  • <localRepository>D:/m2_repo</localRepository>:指定 Maven 使用 D:/m2_repo 作为本地仓库路径。

2.Profiles 定义:

  • 定义了一个 custom-repositories profile,包含了所有需要的仓库:
    • 私有仓库 1(192.168.0.203)
    • 私有仓库 2(192.168.0.66)
    • 阿里云公共仓库

3.Repositories 定义顺序:

  • Maven 会按照定义的顺序依次尝试从这些仓库下载依赖。因此,这个配置确保了 Maven 在下载依赖时会先尝试两个私有仓库,然后再尝试阿里云公共仓库。

4.Active Profiles:

  • <activeProfiles> 块中启用了 custom-repositories profile,使得 Maven 在构建过程中使用这些仓库。

操作步骤:

  • 1.将上述配置添加到 Maven 的 settings.xml 文件中。该文件通常位于用户主目录下的 .m2 目录中(例如:C:\Users\YourUsername.m2\settings.xml 或 ~/.m2/settings.xml)。

  • 2.根据需要修改 settings.xml 文件中的仓库 URL 和本地仓库路径。

  • 3.确保 标签包含 custom-repositories 以启用自定义的仓库配置。

  • 4.保存修改后的 settings.xml 文件。

通过这种方式,IntelliJ IDEA 将会遵循定义的顺序,依次尝试从两个私有仓库和阿里云公共仓库下载依赖。这样可以确保系统在不同的仓库中查找所需的依赖,避免因为某个仓库中缺少依赖而导致构建失败。

Maven官方文档


👑 天下英雄出我辈,一入江湖岁月催 我是热爱生活的「 无间行者 」,努力把实践过的解决方案分享给大家 如果这篇文章对你有用,一键三连,感谢你的鼓励,让我知道你在看


相关推荐
廋到被风吹走2 小时前
【Spring】Spring Data JPA Repository 自动实现机制深度解析
java·后端·spring
MX_93592 小时前
Spring中Bean的配置(一)
java·后端·spring
sg_knight6 小时前
Spring 框架中的 SseEmitter 使用详解
java·spring boot·后端·spring·spring cloud·sse·sseemitter
郑州光合科技余经理8 小时前
同城系统海外版:一站式多语种O2O系统源码
java·开发语言·git·mysql·uni-app·go·phpstorm
一只乔哇噻8 小时前
java后端工程师+AI大模型开发进修ing(研一版‖day60)
java·开发语言·人工智能·学习·语言模型
Dolphin_Home8 小时前
笔记:SpringBoot静态类调用Bean的2种方案(小白友好版)
java·spring boot·笔记
暗然而日章9 小时前
C++基础:Stanford CS106L学习笔记 4 容器(关联式容器)
c++·笔记·学习
盐焗西兰花9 小时前
鸿蒙学习实战之路:Tabs 组件开发场景最佳实践
学习·华为·harmonyos
MetaverseMan9 小时前
Java虚拟线程实战
java
_Kayo_9 小时前
Next.js 路由 简单学习笔记
笔记·学习·next.js