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

GitHub Actions中Maven动态更新版本后因相对路径错误构建失败

Maven多模块项目GitHub Actions构建中versions:set相对路径错误排查

我们的项目通过GitHub Actions实现Maven自动化构建,版本号动态设置为项目版本-runID-runNumber格式(例如8.1.0-9761957358-18),用于部署工作流解析runID并下载构建产物。

在执行mvn versions:set更新版本时,引用父模块使用../../pom.xml的子模块出现「相对路径」错误,但使用../pom.xml的模块无异常。具体错误输出如下:

[ERROR] Child module ... of ... cannot be found in the workspace
[ERROR] The POM for ... is missing, no dependency information available
[FATAL] Non-resolvable parent POM for com.example.project.submodule: Could not find artifact com.example.project:module:pom:8.1.0-9761957358-18 in central (https://repo1.maven.org/maven2) and 'parent.relativePath' points at wrong local POM

项目文件结构

project-root/
│
├── pom.xml
├── module1/
│   ├── pom.xml
│   └── modules/
│       ├── submodule1/
│       │   └── pom.xml
│       ├── submodule2/
│       │   └── pom.xml

当前GitHub Actions工作流

name: Build and Publish

on:
  push:
    paths-ignore: ['.github/**']
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v4

      - name: Set Up JDK 11
        uses: actions/setup-java@v4
        with:
          java-version: 11
          distribution: 'temurin'
          cache: maven

      - name: Set Version
        run: |
          mvn versions:set -DnewVersion=8.1.0-${{ github.run_id }}-${{ github.run_number }} -DgenerateBackupPoms=false

POM文件示例

父POM(project-root/pom.xml)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.project</groupId>
  <artifactId>parent-project</artifactId>
  <version>8.1.0</version>
  <packaging>pom</packaging>
  <modules>
    <module>module1</module>
  </modules>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

模块POM(project-root/module1/pom.xml)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>parent-project</artifactId>
    <groupId>com.example.project</groupId>
    <version>8.1.0</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.project.module1</groupId>
  <artifactId>module1</artifactId>
  <packaging>pom</packaging>
  <modules>
    <module>modules/submodule1</module>
  </modules>
</project>

子模块POM(project-root/module1/modules/submodule1/pom.xml)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>module1</artifactId>
    <groupId>com.example.project.module1</groupId>
    <version>8.1.0</version>
    <relativePath>../../pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.project.module1</groupId>
  <artifactId>submodule1</artifactId>
  <packaging>jar</packaging>
</project>

已尝试的解决方法

  • 将versions:set替换为mvn dependency:tree,仍出现相同的「相对路径」错误
  • 删除GitHub Actions Runner本地Maven仓库清除缓存,无效
  • 移除版本号中的runID,问题依旧
  • 显式逐个安装所有模块POM(mvn install -N),无效
  • 核对父模块的groupId、artifactId和版本号与对应POM文件完全一致
  • 该工作流之前可正常运行,某次合并后失败,怀疑缓存问题但无法确认
  • 构建合并前的提交版本仍报错
  • 在Windows 10虚拟机本地使用相同仓库和POM文件构建成功
  • 通过写保护POM文件尝试更新版本可复现错误,推测构建早期出现问题但无法排查

恳请各位提供解决该问题的思路!


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 15:40:22