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

GitLab Maven Docker流水线Docker构建阶段失败求助

解决GitLab CI中Docker构建阶段找不到Maven产物的问题

问题原因

GitLab CI的每个Job运行在独立环境中,默认不共享文件系统。maven-build阶段生成的target目录仅存在于该Job的工作空间,docker-build阶段无法直接访问,导致Docker构建时找不到目标文件。

解决方案

通过GitLab CI的artifacts机制,将maven-build阶段生成的target目录传递到后续的docker-build阶段。

修改.gitlab-ci.yml配置

在maven-buildJob中添加artifacts配置,指定需要传递的文件路径:

# Define the job that will build the Maven project
maven-build:
  image: maven:3.6.3-jdk-8
  stage: build
  script: "mvn $MAVEN_CLI_OPTS clean deploy -e"
  artifacts:
    paths:
      - target/  # 传递target目录到后续阶段
    expire_in: 1 week  # 可选:设置产物过期时间,避免占用存储资源

完整配置示例

修改后的.gitlab-ci.yml如下:

image: docker:stable

# Define the stages that will be executed in the pipeline
stages:
- build
- package

# Define global variables
variables:
  MAVEN_CLI_OPTS: >
-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
--global-settings $MAVEN_SETTINGS_XML_GITLAB_PACKAGE_REGISTRY
cache:
  key: global-maven-repository-cache
  paths:
    - .m2/repository

# Define the job that will build the Maven project
maven-build:
  image: maven:3.6.3-jdk-8
  stage: build
  script: "mvn $MAVEN_CLI_OPTS clean deploy -e"
  artifacts:
    paths:
      - target/
    expire_in: 1 week

docker-build:
  stage: package
  script:
    - docker info
    - docker build -t gitlab.my-domain.local:5000/spring-boot-hello-world:latest .
    - docker push gitlab.my-domain.local:5000/spring-boot-hello-world:latest

额外检查点

  1. 确认mvn clean deploy命令确实生成了target/${EXECUTABLE_APP_JAR}文件,可查看maven-build阶段日志,检查是否有BUILD SUCCESS及jar包生成记录。
  2. 若使用GitLab Runner的Docker executor,确保Runner配置允许artifacts传递(默认已开启)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 17:23:13