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

本文详细介绍如何在Maven settings.xml中设置多个镜像仓库,以便于项目从多个源自动查找依赖,避免单一仓库限制。通过配置<profiles>和<activeProfiles>实现灵活切换不同仓库的行为。
部署运行你感兴趣的模型镜像

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,pluginRepositoriesandproperties。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>

您可能感兴趣的与本文相关的镜像

EmotiVoice

EmotiVoice

AI应用

EmotiVoice是由网易有道AI算法团队开源的一块国产TTS语音合成引擎,支持中英文双语,包含2000多种不同的音色,以及特色的情感合成功能,支持合成包含快乐、兴奋、悲伤、愤怒等广泛情感的语音。

### 配置多个项目的Maven仓库地址或多模块项目中的Maven仓库设置 在多模块 Maven 项目中,可以通过多种方式来管理和配置不同模块的依赖以及远程仓库地址。以下是具体的实现方法: #### 使用 `settings.xml` 文件全局配置 可以在用户的 `.m2/settings.xml` 或者 Maven 安装目录下的 `conf/settings.xml` 中定义镜像或私有库地址。通过这种方式,所有的 Maven 构建都会自动应用这些配置。 ```xml <mirrors> <mirror> <id>my-private-repo</id> <name>My Private Repository</name> <url>http://repo.mycompany.com/maven2</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> ``` 上述代码片段展示了如何为所有构建指定一个统一的镜像源[^1]。 #### 在父 POM 中集中管理依赖和仓库 对于多模块项目,通常会有一个父 POM 来协调子模块之间的关系。在这种情况下,可以将公共的依赖项声明放在 `<dependencyManagement>` 节点下,并且还可以在此处添加自定义的远程仓库地址。 ```xml <repositories> <repository> <id>custom-repo</id> <url>https://example.com/repo</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>custom-plugin-repo</id> <url>https://example.com/plugin-repo</url> </pluginRepository> </pluginRepositories> ``` 以上 XML 片段说明了如何在父级 POM 文件中定义额外的存储库位置。 #### 子模块继承父 POM 的配置 当每个子模块都指定了自己的 `pom.xml` 并且它们共同拥有同一个父 POM 时,则无需重复定义那些已经在父级设定好的内容。例如 servlet API 可能只需要被某些特定层次(比如 controller 层)所引用即可。 ```xml <!-- maven_service/pom.xml --> <parent> <groupId>com.example.parent</groupId> <artifactId>maven-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <!-- service-specific dependencies here --> </dependencies> ``` ```xml <!-- maven_controller/pom.xml --> <parent> <groupId>com.example.parent</groupId> <artifactId>maven-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies> ``` 这里展示了一个典型的场景,在其中控制器模块单独包含了 Servlet API 的依赖而服务端并没有必要引入它。 #### 动态切换不同的仓库环境 如果存在开发、测试与生产三种环境下各自独立的一套 artifact storage system ,那么也可以利用 profile 技术动态调整实际使用repository URL 。如下所示: ```xml <profiles> <profile> <id>dev-env</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env.repos.url>http://dev-nexus.company.local/content/groups/public/</env.repos.url> </properties> </profile> <profile> <id>prod-env</id> <properties> <env.repos.url>http://prod-artifactory.corporate.org/libs-release-local/</env.repos.url> </properties> </profile> </profiles> <repositories> <repository> <id>central-with-profile</id> <url>${env.repos.url}</url> </repository> </repositories> ``` 这种做法允许开发者依据当前运行状况灵活改变资源获取途径。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值