CI/CD流水线Maven仓库缓存异常:二次运行仍缓存未命中
Maven仓库缓存未命中问题(CI/CD流水线)
问题描述
在CI/CD流水线中使用Maven容器镜像构建时,首次运行后已生成依赖缓存,但二次运行仍出现缓存未命中,导致流水线重复下载所有Maven依赖。
首次运行日志
2024-02-16T06:07:55.0601063Z ##[section]Starting: Cache Maven local repo 2024-02-16T06:07:55.0604747Z ============================================================================== 2024-02-16T06:07:55.0604864Z Task : Cache 2024-02-16T06:07:55.0604921Z Description : Cache files between runs 2024-02-16T06:07:55.0604995Z Version : 2.198.0 2024-02-16T06:07:55.0605064Z Author : Microsoft Corporation 2024-02-16T06:07:55.0605132Z Help : https://aka.ms/pipeline-caching-docs 2024-02-16T06:07:55.0605208Z ============================================================================== 2024-02-16T06:07:55.4414221Z Resolving key: 2024-02-16T06:07:55.4553411Z - "funcs" [string] 2024-02-16T06:07:55.4571469Z - maven [string] 2024-02-16T06:07:55.4571795Z - "Linux" [string] 2024-02-16T06:07:55.5018041Z - pom.xml [file] --> 5893AE16A5F30361F4225A94E477DB4CD1996348660AD3CFBB1F4DBF72753257 2024-02-16T06:07:55.5064081Z Resolved to: "funcs"|maven|"Linux"|EpdW7VOO9P62oHHqI9TxsjUKX3NemXThFSh/NjDbSf8= 2024-02-16T06:07:55.5126152Z Resolving restore key: 2024-02-16T06:07:55.5126751Z - maven [string] 2024-02-16T06:07:55.5127024Z - "Linux" [string] 2024-02-16T06:07:55.5133145Z Resolved to: maven|"Linux"|** 2024-02-16T06:07:55.5133337Z Resolving restore key: 2024-02-16T06:07:55.5133577Z - maven [string] 2024-02-16T06:07:55.5133821Z Resolved to: maven|** 2024-02-16T06:07:55.5592310Z Using default max parallelism. 2024-02-16T06:07:56.4382488Z Using default max parallelism. 2024-02-16T06:07:56.4391109Z Max dedup parallelism: 192 2024-02-16T06:07:56.4391465Z DomainId: 0 2024-02-16T06:07:56.5918440Z ApplicationInsightsTelemetrySender will correlate events with X-TFS-Session 2c5e36a2-f0aa-46fc-8d04-fce0bc3c204d 2024-02-16T06:07:56.5943617Z Hashtype: Dedup64K 2024-02-16T06:07:56.7734517Z Getting a pipeline cache artifact with one of the following fingerprints: 2024-02-16T06:07:56.7735134Z Fingerprint: `"funcs"|maven|"Linux"|EpdW7VOO9P62oHHqI9TxsjUKX3NemXThFSh/NjDbSf8=` 2024-02-16T06:07:56.7735886Z Fingerprint: `maven|"Linux"|**` 2024-02-16T06:07:56.7736226Z Fingerprint: `maven|**` 2024-02-16T06:07:57.3874838Z There is a cache miss. 2024-02-16T06:07:58.0799644Z ApplicationInsightsTelemetrySender correlated 1 events with X-TFS-Session 2c5e36a2-f0aa-46fc-8d04-fce0bc3c204d 2024-02-16T06:07:58.0924753Z ##[section]Finishing: Cache Maven local repo
二次运行日志
2024-02-16T06:21:28.1316213Z ##[section]Starting: Cache Maven local repo 2024-02-16T06:21:28.1319938Z ============================================================================== 2024-02-16T06:21:28.1320071Z Task : Cache 2024-02-16T06:21:28.1320128Z Description : Cache files between runs 2024-02-16T06:21:28.1320203Z Version : 2.198.0 2024-02-16T06:21:28.1320263Z Author : Microsoft Corporation 2024-02-16T06:21:28.1320345Z Help : https://aka.ms/pipeline-caching-docs 2024-02-16T06:21:28.1320422Z ============================================================================== 2024-02-16T06:21:28.4851948Z Resolving key: 2024-02-16T06:21:28.5007633Z - "funcs" [string] 2024-02-16T06:21:28.5025467Z - maven [string] 2024-02-16T06:21:28.5026109Z - "Linux" [string] 2024-02-16T06:21:28.5434800Z - pom.xml [file] --> 5893AE16A5F30361F4225A94E477DB4CD1996348660AD3CFBB1F4DBF72753257 2024-02-16T06:21:28.5478850Z Resolved to: "funcs"|maven|"Linux"|EpdW7VOO9P62oHHqI9TxsjUKX3NemXThFSh/NjDbSf8= 2024-02-16T06:21:28.5537145Z Resolving restore key: 2024-02-16T06:21:28.5537814Z - maven [string] 2024-02-16T06:21:28.5538343Z - "Linux" [string] 2024-02-16T06:21:28.5543267Z Resolved to: maven|"Linux"|** 2024-02-16T06:21:28.5543652Z Resolving restore key: 2024-02-16T06:21:28.5544353Z - maven [string] 2024-02-16T06:21:28.5544566Z Resolved to: maven|** 2024-02-16T06:21:28.5983821Z Using default max parallelism. 2024-02-16T06:21:29.4158957Z Using default max parallelism. 2024-02-16T06:21:29.4167148Z Max dedup parallelism: 192 2024-02-16T06:21:29.4169349Z DomainId: 0 2024-02-16T06:21:29.5509867Z ApplicationInsightsTelemetrySender will correlate events with X-TFS-Session e570c636-2828-41f0-a57d-df6db6a4a793 2024-02-16T06:21:29.5531316Z Hashtype: Dedup64K 2024-02-16T06:21:29.8068742Z Getting a pipeline cache artifact with one of the following fingerprints: 2024-02-16T06:21:29.8069565Z Fingerprint: `"funcs"|maven|"Linux"|EpdW7VOO9P62oHHqI9TxsjUKX3NemXThFSh/NjDbSf8=` 2024-02-16T06:21:29.8070600Z Fingerprint: `maven|"Linux"|**` 2024-02-16T06:21:29.8070948Z Fingerprint: `maven|**` 2024-02-16T06:21:30.8048244Z There is a cache miss. 2024-02-16T06:21:31.6162213Z ApplicationInsightsTelemetrySender correlated 1 events with X-TFS-Session e570c636-2828-41f0-a57d-df6db6a4a793 2024-02-16T06:21:31.6285835Z ##[section]Finishing: Cache Maven local repo
流水线配置
trigger: branches: include: - "master" variables: MAVEN_CACHE_FOLDER: $(MAVEN_HOME).m2/repository MAVEN_OPTS: "-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)" stages: - stage: Build displayName: "CI" jobs: - job: Build displayName: "Build/Test Service" pool: vmImage: ubuntu-latest container: image: maven:3.9-eclipse-temurin-21 steps: - checkout: self fetchDepth: 0 - task: Cache@2 displayName: "Cache Maven local repo" inputs: key: '"funcs" | maven | "$(Agent.OS)" | pom.xml' restoreKeys: | maven | "$(Agent.OS)" maven path: $(MAVEN_CACHE_FOLDER) - task: Maven@3 inputs: mavenPomFile: "pom.xml" goals: "clean install" options: "-DskipTests $(MAVEN_OPTS)" displayName: "Maven Install"
问题分析与解决方案
核心问题
配置中MAVEN_CACHE_FOLDER变量的定义缺少斜杠,导致路径拼接错误:
- 原配置:
$(MAVEN_HOME).m2/repository - 在maven容器中,
MAVEN_HOME默认值为/usr/share/maven,拼接后路径变为/usr/share/maven.m2/repository——这是一个不存在的路径,Maven实际不会将依赖下载到该目录,因此Cache任务无法捕获到依赖文件,自然无法生成有效缓存。
修复步骤
修正缓存路径变量
将MAVEN_CACHE_FOLDER改为:MAVEN_CACHE_FOLDER: $(MAVEN_HOME)/.m2/repository添加斜杠后,路径变为正确的
/usr/share/maven/.m2/repository,与Maven容器内的默认仓库路径一致。确保缓存目录存在
可在Cache任务前添加步骤创建目录,避免路径不存在导致的异常:- script: mkdir -p $(MAVEN_CACHE_FOLDER) displayName: "Create Maven cache directory"验证缓存逻辑
Cache@2任务会在流水线步骤结束后自动检测path指定目录的变化,将新增文件上传到缓存系统。修正路径后,Maven下载的依赖会被正确捕获,二次运行时即可命中缓存。
内容的提问来源于stack exchange,提问作者twerpiebird
相关产品推荐
相关产品推荐

