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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章