You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非父属性聚合Maven项目:如何读取Nexus中的versions.yaml?

统一管理多Maven测试项目的依赖版本

我有数十个独立的自动化测试Maven项目,这些项目大多依赖其他项目(因为它们测试同一个大型系统的不同模块)。维护这些依赖版本的一致性几乎成了全职工作。我想创建一个可版本化的聚合项目来统一管理所有版本号,但直接在聚合项目里维护依赖会带来以下问题:

  • a) 每个项目都会下载所有其他项目,哪怕只需要其中一两个
  • b) 可能会下载自身的旧版本,引发冲突
  • c) 版本管理不便

出于a和b的原因,我不想用链式依赖,也不想把父pom扩展成庞大的文件。我了解Properties Maven Plugin,计划创建一个包含pom.xml和versions.yaml文件的独立项目并部署到Nexus,versions.yaml内容示例如下:

project-1: 0.4.2-SNAPSHOT
project-2: 0.9.18-SNAPSHOT

测试项目中的依赖会写成这种形式:

<dependency>
  <groupId>group</groupId>
  <artifactId>artifact</artifactId>
  <version>${project-1}</version>
</dependency>

但我不确定怎么在其他项目中通过Properties Maven Plugin访问这个versions.yaml文件,配置里的<file>路径该怎么填:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>properties-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>read-project-properties</goal>
      </goals>
      <configuration>
        <files>
          <file>???</file>  <!--TODO-->
        </files>
      </configuration>
    </execution>
  </executions>
</plugin>

解决方法

要实现从Nexus部署的独立项目中读取versions.yaml,需先将这个配置项目作为依赖引入测试项目,再通过Maven依赖机制定位文件,具体步骤如下:

1. 配置独立版本管理项目的pom.xml

确保版本管理项目(包含versions.yaml)打包时,将versions.yaml纳入JAR的resources目录。修改其pom.xml:

<project>
  <groupId>your.group.id</groupId>
  <artifactId>version-properties</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <build>
    <resources>
      <resource>
        <directory>${project.basedir}</directory>
        <includes>
          <include>versions.yaml</include>
        </includes>
      </resource>
    </resources>
  </build>
</project>

执行mvn deploy将该项目发布到Nexus仓库。

2. 在测试项目中引入版本管理项目作为依赖

在测试项目的pom.xml中添加依赖,scope设为provided或compile(确保构建阶段可访问即可):

<dependencies>
  <dependency>
    <groupId>your.group.id</groupId>
    <artifactId>version-properties</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

3. 配置插件读取Jar中的文件

先用maven-dependency-plugin将版本管理JAR中的versions.yaml解压到构建目录,再用Properties插件读取。修改插件配置:

<build>
  <plugins>
    <!-- 解压版本管理JAR中的versions.yaml -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>3.6.0</version>
      <executions>
        <execution>
          <id>unpack-version-properties</id>
          <phase>initialize</phase>
          <goals>
            <goal>unpack</goal>
          </goals>
          <configuration>
            <artifactItems>
              <artifactItem>
                <groupId>your.group.id</groupId>
                <artifactId>version-properties</artifactId>
                <version>1.0.0-SNAPSHOT</version>
                <outputDirectory>${project.build.directory}/version-props</outputDirectory>
                <includes>versions.yaml</includes>
              </artifactItem>
            </artifactItems>
          </configuration>
        </execution>
      </executions>
    </plugin>

    <!-- 读取解压后的versions.yaml -->
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>properties-maven-plugin</artifactId>
      <version>1.2.1</version>
      <executions>
        <execution>
          <phase>initialize</phase>
          <goals>
            <goal>read-project-properties</goal>
          </goals>
          <configuration>
            <files>
              <file>${project.build.directory}/version-props/versions.yaml</file>
            </files>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

4. 可选:统一版本管理项目的版本

为避免重复编写版本管理项目的版本,可将其定义为Maven属性:

<properties>
  <version-properties.version>1.0.0-SNAPSHOT</version-properties.version>
</properties>

<!-- 依赖和插件配置中使用${version-properties.version} -->

补充说明

  • Properties Maven Plugin从1.0版本开始支持读取YAML格式文件,无需额外配置解析规则。
  • 确保maven-dependency-plugin版本足够新,以保证includes参数正常工作。
  • 版本管理项目更新时,只需发布新版本,修改测试项目中对应的版本号即可同步所有依赖版本。

内容的提问来源于stack exchange,提问作者Petersaber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 13:53:16