Speeding Up Azure DevOps Pipelines with Dependency Caching
Azure DevOps Pipelines can dramatically shorten dependency restore times with the built‑in Cache@2 task. This blog walks through why caching matters, how to set it up for Maven, how to verify it works, and the trade‑offs you need to watch out for.
09 Aug 2026, 02:57 UTC

Why Dependency Caching Matters
In a typical CI/CD workflow, the first step after the source checkout is usually a dependency restore – pulling Maven, Gradle, npm, or NuGet packages from external registries. For medium‑sized Java or .NET projects, this can take 1‑3 minutes, bloating the overall pipeline and delaying feedback for developers. Azure DevOps Pipelines offers a built‑in Cache@2 task that can store these downloaded artifacts between runs, cutting the restore time by 40‑70 % in many real‑world scenarios.
How the Cache Works
The cache task stores a folder or file on Azure DevOps’ distributed storage. On subsequent runs, the task restores the cached content to the specified location before the restore step begins. The key that identifies a cache entry is user‑defined; Azure DevOps will create a new cache if the key changes, which is useful for automatically invalidating the cache when dependencies change.
Key Concepts
- Key: A string that uniquely identifies the cache. Common practice is to hash a lock file (e.g.,
pom.xmlfor Maven orpackages.lock.jsonfor .NET) so that any dependency change busts the cache. - Path: The file system location to cache. For Maven, this is typically
~/.m2/repositoryon Linux agents. - Scope: Caches are shared across all pipeline runs in an organization, but the key ensures isolation per project or dependency set.
Concrete Example: Caching Maven Dependencies
The following YAML snippet demonstrates how to add a cache task before the mvn dependency:go-offline step in a Java project. Replace YOUR_PROJECT with the actual project name and $BUILD_SOURCESDIRECTORY with the default source folder placeholder.
- task: Cache@2
displayName: 'Cache Maven Dependencies'
inputs:
key: 'maven | $(Agent.OS) | $(Build.SourcesDirectory)/pom.xml'
path: $(M2_HOME)/repository
cacheHitVar: CACHE_RESTORED
- script: |
mvn -B dependency:go-offline
displayName: 'Restore Maven Dependencies'
condition: eq(variables.CACHE_RESTORED, 'false')
Explanation of the snippet:
keycombines the operating system and the SHA‑1 checksum ofpom.xml. If the file changes, the key changes and a cache miss occurs.pathpoints to the local Maven repository directory. Azure DevOps will restore this folder if a cache hit is found.- The
cacheHitVaroutputs a boolean variable (CACHE_RESTORED) that you can use to skip the restore step when the cache hits. Theconditionon the Maven script ensures the restore runs only on a miss.
Verifying Cache Effectiveness
- Check the logs: After a pipeline run, look for the
Cache@2task output. A line statingCache hitorCache missconfirms whether the cache was used. - Measure restore time: Compare the duration of the
mvn dependency:go-offlinestep before and after enabling the cache. A consistent 40‑70 % reduction indicates the cache is working. - Inspect cache usage: In the Azure DevOps portal, navigate to
Organization Settings → Pipelines → Cacheto view current storage usage. Ensure you stay below the 10 GB limit; otherwise, older caches will be evicted.
Trade‑Offs and Limitations
- Storage limits: Each organization has a 10 GB quota. Large projects or many different cache keys can quickly exhaust this space, leading to frequent misses.
- Key design: Using a static key (e.g.,
maven | $(Agent.OS)) will reuse stale dependencies, potentially breaking builds or exposing security vulnerabilities. Always tie the key to a lock file or checksum. - Cache consistency: If a dependency is added or removed but the key doesn’t change (e.g., editing
pom.xmlbut the checksum remains the same due to whitespace changes), the cache may still be considered valid, causing subtle bugs. - Agent differences: The
$(M2_HOME)variable may differ between Windows and Linux agents; include the OS in the key to avoid cross‑platform cache collisions.
Actionable Takeaway
Implement the Cache@2 task in your Azure DevOps pipelines early in the build process, using a lock‑file checksum as the key. After adding the task, run a few builds, inspect the logs for cache hits, and compare the restore times. If you hit the 10 GB quota, consider pruning unused caches from the portal or consolidating cache keys. With a proper cache strategy, you can shave minutes off every pipeline run and give developers faster feedback.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.