<p>I forgot to add a MAVEN_OPTS variable in mavenOptions:<br>
    <code>mavenOptions: '-Xmx3072m $(MAVEN_OPTS)'</code></p>

<p>My pipeline.yml stayed like this:  </p>

<pre><code>trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

variables:
  MAVEN_CACHE_FOLDER: $(Pipeline.Workspace)/.m2/repository
  MAVEN_OPTS: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'


steps:

- task: Cache@2
  inputs:
    key: 'maven | "$(Agent.OS)" | **/pom.xml'
    restoreKeys: |
      maven | "$(Agent.OS)"
      maven
    path: $(MAVEN_CACHE_FOLDER)
  displayName: Cache Maven local repo

- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m $(MAVEN_OPTS)'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'clean package'
    codeCoverageToolOption: JaCoCo
    sonarQubeRunAnalysis: false
- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)'
    publishLocation: 'pipeline'

- script: mvn install -B -e
</code></pre>

CC BY-SA 4.0
2020-05-15T19:47:53.217Z
2020-05-15T19:47:53.217Z
danilo
<p>I made it work like this:</p>
<pre><code>variables:
  MAVEN_CACHE_FOLDER: $(HOME)/.m2/repository
  MAVEN_OPTS: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:
  - task: Cache@2
    displayName: 'Cache Maven local repo'
    inputs:
      key: '&quot;funcs&quot; | maven | &quot;$(Agent.OS)&quot; | pom.xml'
      restoreKeys: |
      path: $(MAVEN_CACHE_FOLDER)

  - task: Maven@3
    displayName: 'Build and Test'
    inputs:
      mavenPomFile: 'pom.xml'
      mavenOptions: '-Xmx2048m'
      javaHomeOption: 'JDKVersion'
      jdkVersionOption: '1.11'
      jdkArchitectureOption: 'x64'
      publishJUnitResults: true
      testResultsFiles: '**/surefire-reports/TEST-*.xml'
      goals: '-B package'

  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage'
    inputs:
      codeCoverageTool: 'JaCoCo'
      summaryFileLocation: '$(System.DefaultWorkingDirectory)/target/site/jacoco/jacoco.xml'
      pathToSources: '$(System.DefaultWorkingDirectory)/src/main/java'
      additionalCodeCoverageFiles: '$(System.DefaultWorkingDirectory)/target/jacoco.exec'
</code></pre>
<p>It has:</p>
<ul>
<li>caching of the Maven repository</li>
<li>Run maven with <code>-B</code> to avoid download texts in the output</li>
<li>Use Java 11</li>
<li>Publish test reports</li>
<li>Publish Jacoco code coverage</li>
</ul>

CC BY-SA 4.0
2022-12-27T13:58:32.813Z
About the line "path: $(MAVEN_CACHE_FOLDER)", is the identation correct?
Paulo Pedroso
CC BY-SA 4.0
2021-07-16T13:49:36.640Z
2021-07-16T13:49:36.640Z
Wim Deblauwe
<p>I want to avoid maven downloading all dependencies from a remote repository in all build  </p>

<p>I configured a cache, but all executions still download all dependencies.  </p>

<p><a href="https://i.sstatic.net/adP3E.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/adP3E.png" alt="enter image description here"></a></p>

<p>azure-pipelines.yml  </p>

<pre><code>trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

variables:
  MAVEN_CACHE_FOLDER: $(Pipeline.Workspace)/.m2/repository
  MAVEN_OPTS: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'

steps:
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'clean package'
    codeCoverageToolOption: JaCoCo
    sonarQubeRunAnalysis: false
- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)'
    publishLocation: 'pipeline'
- task: Cache@2
  inputs:
    key: 'maven | "$(Agent.OS)" | **/pom.xml'
    restoreKeys: |
      maven | "$(Agent.OS)"
      maven
    path: $(MAVEN_CACHE_FOLDER)
  displayName: Cache Maven local repo

- script: mvn install -B -e
</code></pre>

<azure-pipelines><maven><repository>
Using cached dependencies for pipelines in Azure Devops
danilo
# Using cached dependencies for pipelines in Azure Devops

(Asked by: username_0 on 2020-05-15)

I want to avoid maven downloading all dependencies from a remote repository in all build  
I configured a cache, but all executions still download all dependencies.  

azure-pipelines.yml  
trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

variables:
  MAVEN_CACHE_FOLDER: $(Pipeline.Workspace)/.m2/repository
  MAVEN_OPTS: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'

steps:
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'clean package'
    codeCoverageToolOption: JaCoCo
    sonarQubeRunAnalysis: false
- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)'
    publishLocation: 'pipeline'
- task: Cache@2
  inputs:
    key: 'maven | "$(Agent.OS)" | **/pom.xml'
    restoreKeys: |
      maven | "$(Agent.OS)"
      maven
    path: $(MAVEN_CACHE_FOLDER)
  displayName: Cache Maven local repo

- script: mvn install -B -e

**Tags:** azure-pipelines, maven, repository


---

## 2 answers

### Answer by username_0 on 2020-05-15. Score: 1

I forgot to add a MAVEN_OPTS variable in mavenOptions:
mavenOptions: '-Xmx3072m $(MAVEN_OPTS)'
My pipeline.yml stayed like this:  
trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

variables:
  MAVEN_CACHE_FOLDER: $(Pipeline.Workspace)/.m2/repository
  MAVEN_OPTS: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'


steps:

- task: Cache@2
  inputs:
    key: 'maven | "$(Agent.OS)" | **/pom.xml'
    restoreKeys: |
      maven | "$(Agent.OS)"
      maven
    path: $(MAVEN_CACHE_FOLDER)
  displayName: Cache Maven local repo

- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m $(MAVEN_OPTS)'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'clean package'
    codeCoverageToolOption: JaCoCo
    sonarQubeRunAnalysis: false
- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)'
    publishLocation: 'pipeline'

- script: mvn install -B -e


---

### Answer by username_1 on 2021-07-16. Score: 1

I made it work like this:
variables:
  MAVEN_CACHE_FOLDER: $(HOME)/.m2/repository
  MAVEN_OPTS: '-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)'

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:
  - task: Cache@2
    displayName: 'Cache Maven local repo'
    inputs:
      key: '"funcs" | maven | "$(Agent.OS)" | pom.xml'
      restoreKeys: |
      path: $(MAVEN_CACHE_FOLDER)

  - task: Maven@3
    displayName: 'Build and Test'
    inputs:
      mavenPomFile: 'pom.xml'
      mavenOptions: '-Xmx2048m'
      javaHomeOption: 'JDKVersion'
      jdkVersionOption: '1.11'
      jdkArchitectureOption: 'x64'
      publishJUnitResults: true
      testResultsFiles: '**/surefire-reports/TEST-*.xml'
      goals: '-B package'

  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage'
    inputs:
      codeCoverageTool: 'JaCoCo'
      summaryFileLocation: '$(System.DefaultWorkingDirectory)/target/site/jacoco/jacoco.xml'
      pathToSources: '$(System.DefaultWorkingDirectory)/src/main/java'
      additionalCodeCoverageFiles: '$(System.DefaultWorkingDirectory)/target/jacoco.exec'

It has:

caching of the Maven repository
Run maven with -B to avoid download texts in the output
Use Java 11
Publish test reports
Publish Jacoco code coverage

> **Comments:**
> **username_2**: About the line "path: $(MAVEN_CACHE_FOLDER)", is the identation correct?  

---

