公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

Maven配置多个仓库共同使用的方法

 

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

最终的settings.xml文件配置

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

<?xml version="1.0" encoding="UTF-8"?>
<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个二级子标签:activation, repositories, pluginRepositories 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>

分享到:
标签:配置 Maven
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定