The Gradle build is failing in Jenkins because the Jenkins agent is unable to acquire the necessary lock on the Gradle daemon, preventing it from executing tasks.

This often happens due to stale daemon processes holding onto locks from previous, possibly interrupted, builds.

Here’s how to tackle it, from most to least common:

1. Clean the Gradle Cache and Daemons

Diagnosis: A quick way to see if stale daemons are the issue is to try a clean build. If it works after this, you’ve likely found your culprit.

Fix: On your Jenkins agent’s workspace directory, run:

./gradlew --stop
./gradlew clean build

The --stop command explicitly terminates any running Gradle daemons associated with that project. The clean build then starts fresh.

Why it works: Stale daemon processes are the most frequent offenders. They might be stuck in an error state from a previous build, or simply not shutting down cleanly, holding onto essential file locks that prevent new builds from starting.

2. Increase Gradle Daemon Memory

Diagnosis: If you see errors related to OutOfMemoryError within the Gradle daemon logs (often found in ~/.gradle/daemon/<daemon-version>/), or if builds consistently fail after a certain amount of time, the daemon might be running out of memory.

Fix: In your gradle.properties file (either in the project root or in ~/.gradle/ for global settings), add or modify these lines:

org.gradle.jvmargs=-Xmx4096m -XX:MaxMetaspaceSize=1024m

This allocates 4GB of heap space and 1GB of metaspace to the Gradle daemon JVM. Adjust 4096m and 1024m based on your agent’s available RAM and build complexity.

Why it works: Complex builds, especially those with many dependencies or large source sets, can exhaust the default memory allocated to the Gradle daemon. Increasing these limits provides the daemon with more resources to handle the workload.

3. Check File System Permissions

Diagnosis: Errors mentioning "Permission denied" when trying to write to files or directories within the build process, especially in shared Jenkins agent environments, point to permission issues.

Fix: Ensure the user running the Jenkins agent process has read and write permissions for the entire workspace directory and any directories Gradle might use for caching (e.g., ~/.gradle/caches).

# Example: Assuming Jenkins user is 'jenkins'
chown -R jenkins:jenkins /path/to/jenkins/workspace
chown -R jenkins:jenkins /home/jenkins/.gradle

Replace jenkins with the actual user and jenkins:jenkins with the appropriate user and group.

Why it works: Gradle needs to create, modify, and delete files during the build, including temporary files, cached artifacts, and output. If the user running the build lacks the necessary permissions, these operations will fail.

4. Verify Gradle Wrapper Version Consistency

Diagnosis: Inconsistent gradle-wrapper.jar or gradle-wrapper.properties files across different branches or between your local environment and the Jenkins agent can lead to unexpected behavior and build failures.

Fix: Ensure that the gradle-wrapper.jar and gradle-wrapper.properties in your project’s gradle/wrapper/ directory are committed and consistently used. If issues persist, try regenerating them:

# In your project root
gradle wrapper --gradle-version 7.5.1 # Use your desired version

Then commit the updated files.

Why it works: The Gradle Wrapper is responsible for downloading and using the correct Gradle version for your project. Discrepancies can cause the daemon to start with the wrong configuration or fail to initialize properly.

5. Network Connectivity and Proxy Issues

Diagnosis: If your build relies on downloading dependencies from external repositories (like Maven Central, JCenter, or internal Artifactory/Nexus) and you see timeouts or connection refused errors during dependency resolution, network issues are likely.

Fix: Ensure your Jenkins agent has proper network access to all required repositories. If behind a proxy, configure Gradle to use it. This is typically done in ~/.gradle/gradle.properties on the agent:

systemProp.http.proxyHost=your.proxy.host
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=your_proxy_user
systemProp.http.proxyPassword=your_proxy_password
systemProp.https.proxyHost=your.proxy.host
systemProp.https.proxyPort=8080
systemProp.https.proxyUser=your_proxy_user
systemProp.https.proxyPassword=your_proxy_password

Replace placeholders with your actual proxy details.

Why it works: Gradle needs to download dependencies. If it can’t reach the repositories due to network misconfiguration, firewalls, or incorrect proxy settings, the build will fail during the dependency fetching phase.

6. Disk Space on the Agent

Diagnosis: Errors indicating "No space left on device" or similar disk full messages, especially during large builds or when caching is enabled, are a clear sign.

Fix: Free up disk space on the Jenkins agent where the build is running. This might involve clearing old build artifacts, cleaning the Gradle cache (~/.gradle/caches), or increasing the disk allocation for the agent.

Why it works: Gradle caches dependencies and build outputs. Large projects or numerous builds can consume significant disk space. Running out of space prevents Gradle from writing necessary files.

If you’ve addressed all these, the next error you’ll likely encounter is a java.lang.OutOfMemoryError in a deeply nested dependency compilation, indicating you need to tune build-specific JVM arguments or optimize your build tasks.

Want structured learning?

Take the full Jenkins course →