Issue
I'm having problems with the maven cache in azure-pipelines, even downloading the cache and being able to see that the m2/repository folder exists inside the work, when calling the maven task the files are downloaded again, thus invalidating my attempt to use the cache .
MAVEN_CACHE_FOLDER: $(Pipeline.Workspace)/.m2/repository
Where I had the same error and after some research I found this topic in the community microsoft developercommunity which suggested changing the directory used to create the env MAVEN_CACHE_FOLDER
But that didn't solve my problem. Below is my script:
pool:
vmImage: ubuntu-latest
variables:
MAVEN_CACHE_FOLDER: $(HOME)/.m2/repository
MAVEN_OPTS: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'
stages:
- stage: validate
displayName: VALIDATE
jobs:
- job: java_validations
displayName: Java Validations
workspace:
clean: outputs
steps:
- checkout: self
clean: false
- task: Cache@2
displayName: 'Cache Maven local repo'
inputs:
key: '"funcs" | maven | "$(Agent.OS)" | pom.xml'
restoreKeys: |
path: $(MAVEN_CACHE_FOLDER)
- script: |
mvn validate $(MAVEN_OPTS)
displayName: Maven Validate
- script: |
mvn checkstyle:check $(MAVEN_OPTS)
displayName: Maven Checkstyle
- script: |
tree $(MAVEN_CACHE_FOLDER)
displayName: show MAVEN_CACHE_FOLDER tree
- job: unit_tests
displayName: Unit Tests
dependsOn:
- java_validations
workspace:
clean: outputs
steps:
- checkout: self
clean: false
- task: Cache@2
displayName: 'Cache Maven local repo'
inputs:
key: '"funcs" | maven | "$(Agent.OS)" '
restoreKeys: |
path: $(MAVEN_CACHE_FOLDER)
- script: |
tree $(MAVEN_CACHE_FOLDER)
displayName: show MAVEN_CACHE_FOLDER tree
- task: Maven@3
inputs:
mavenPomFile: 'pom.xml'
options: 'test-compile failsafe:integration-test -Dcheckstyle.skip -Pun-tests $(MAVEN_OPTS)'
publishJUnitResults: false
mavenVersionOption: 'Default'
mavenAuthenticateFeed: true
effectivePomSkip: false
sonarQubeRunAnalysis: true
sqMavenPluginVersionChoice: 'latest'
env:
JAVA_HOME: $(JAVA_HOME_17_X64)
PATH: $(JAVA_HOME_17_X64)/bin:$(PATH)
- script: |
tree $(MAVEN_CACHE_FOLDER)
displayName: show MAVEN_CACHE_FOLDER tree
Solution
After contacting the people at microsoft, we arrived at the solution below, where the problem was in the way I was passing the options in the maven task, the property where I must pass is:
mavenOptions: '$(MAVEN_OPTS)'
looking like this:
- task: Maven@3
inputs:
mavenPomFile: 'pom.xml'
mavenOptions: '-Xmx3072m $(MAVEN_OPTS)'
options: 'test-compile failsafe:integration-test -Dcheckstyle.skip -Pun-tests $(MAVEN_OPTS)'
publishJUnitResults: false
mavenVersionOption: 'Default'
mavenAuthenticateFeed: true
effectivePomSkip: false
sonarQubeRunAnalysis: true
sqMavenPluginVersionChoice: 'latest'
env:
JAVA_HOME: $(JAVA_HOME_17_X64)
PATH: $(JAVA_HOME_17_X64)/bin:$(PATH)
This solved it for me and the cache started working.
Answered By - Diego Cândido da Silva
Answer Checked By - Senaida (JavaFixing Volunteer)