Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make one single `.gradle` cache for multiple projects?

We are trying to use one single .gradle cache among our multiple build workers (in jenkins) by creating .gradle in NFS mount which is shared with all the workers.

Now when we run multiple projects using gradle builds, they get failed with following errors:

Timeout waiting to lock artifact cache (/common/user/.gradle/caches/modules-2). It is currently in use by another Gradle instance.
Owner PID: 1XXXX
Our PID: 1XXXX
Owner Operation: resolve configuration ':classpath’
Our operation: resolve configuration ':classpath’
Lock file: /common/user/.gradle/caches/modules-2/modules-2.lock

What is the suggestive method to use .gradle cache sharing among multiple users. This model works fine for maven .m2 cache.

We cannot have .gradle for each workers as it occupies lot of space to store the jars in cache.

like image 869
m18unet Avatar asked Sep 04 '25 01:09

m18unet


1 Answers

Because of the locking mechanism Gradle uses for its dependency cache, you can't have multiple instances write to the same cache directory.

However, you can create a shared, read-only dependency cache that can be used by multiple Gradle instances. You can find instructions in the docs. The basic mechanism is to create a folder that's pre-populated with the dependencies you think your builds will need, then set the GRADLE_RO_DEP_CACHE environment variable to point to that folder.

This cache, unlike the classical dependency cache, is accessed without locking, making it possible for multiple builds to read from the cache concurrently.

Because this cache is read-only, you would need to add dependencies to it beforehand. The builds themselves can't write their dependencies back to the read-only shared cache. The cache needs to follow the folder structure that Gradle expects, though, which isn't something that can really be set up by hand. In practice the way to get a working shared cache is to copy the dependency cache that was created by an existing Gradle instance.

The read-only cache should be sourced from a Gradle dependency cache that already contains some of the required dependencies. [...] In a CI environment, it’s a good idea to have one build which "seeds" a Gradle dependency cache, which is then copied to a different directory. This directory can then be used as the read-only cache for other builds.

The shared cache doesn't need to contain all of the dependencies, though. Any that are missing will be fetched by each individual build as normal, as if the shared cache wasn't there.

https://docs.gradle.org/current/userguide/dependency_resolution.html#sub:shared-readonly-cache

like image 179
Sam Avatar answered Sep 07 '25 10:09

Sam