# How to Fix Gradle Build Failed Error
Your Gradle build fails with one of these common errors:
``` FAILURE: Build failed with an exception.
* What went wrong: Could not resolve all files for configuration ':compileClasspath'. > Could not find com.example:some-library:1.0.0. Required by: project : ```
Or:
* What went wrong:
Execution failed for task ':compileJava'.
> Could not resolve all dependencies for configuration ':compileClasspath'.
> Could not resolve com.example:lib:1.0.0.
> Could not GET 'https://repo.maven.apache.org/maven2/com/example/lib/1.0.0/lib-1.0.0.pom'.Or:
* What went wrong:
Could not determine the dependencies of task ':jar'.
> Could not resolve all files for configuration ':runtimeClasspath'.
> Could not find com.example:library:2.0.0.Gradle build failures stem from dependency issues, configuration problems, or environment issues.
Diagnosis Steps
Step 1: Run with Detailed Logging
```bash # Run with stack trace ./gradlew build --stacktrace
# Run with full debug info ./gradlew build --debug
# Run with info level logging ./gradlew build --info ```
Step 2: Check Dependency Tree
```bash # Show all dependencies ./gradlew dependencies
# Show specific configuration ./gradlew dependencies --configuration compileClasspath ./gradlew dependencies --configuration runtimeClasspath
# Show dependency insight for specific dependency ./gradlew dependencyInsight --dependency com.example:library ```
Step 3: Check Available Tasks
```bash # List all tasks ./gradlew tasks
# List build tasks specifically ./gradlew tasks --group="build" ```
Step 4: Verify Gradle and Java Versions
```bash # Gradle version ./gradlew --version
# Java version used by Gradle java -version
# Check JAVA_HOME echo $JAVA_HOME ```
Step 5: Check Repository Configuration
# View effective configuration
./gradlew properties | grep -A 10 repositoriesSolutions
Solution 1: Fix Dependency Not Found
Add or correct repository in build.gradle:
```groovy repositories { // Primary: Maven Central mavenCentral()
// For Google libraries google()
// For JitPack (GitHub projects) maven { url 'https://jitpack.io' }
// For Spring milestones maven { url 'https://repo.spring.io/milestone' }
// For JBoss maven { url 'https://repository.jboss.org/nexus/content/groups/public/' } } ```
Verify dependency coordinates:
```groovy dependencies { // Check exact format: group:artifact:version implementation 'com.example:library:1.0.0'
// For platform/BOM imports implementation platform('com.example:bom:1.0.0')
// For local JAR files implementation files('libs/my-library.jar')
// For local directory implementation fileTree('libs') { include '*.jar' } } ```
Solution 2: Fix Version Conflicts
When dependencies conflict:
```groovy dependencies { // Force specific version implementation('com.example:library') { version { strictly '2.0.0' } }
// Or use force (deprecated but works) implementation('com.example:library:2.0.0') { force = true } } ```
Use resolution strategy:
```groovy configurations.all { resolutionStrategy { // Force specific version for all dependencies force 'com.example:library:2.0.0'
// Fail on version conflict failOnVersionConflict()
// Prefer modules from specific source preferProjectModules() } } ```
Solution 3: Clear Gradle Caches
```bash # Clear project build cache ./gradlew clean rm -rf build/ rm -rf .gradle/
# Clear global Gradle cache rm -rf ~/.gradle/caches/ rm -rf ~/.gradle/wrapper/dists/
# Rebuild with fresh cache ./gradlew build --refresh-dependencies ```
Solution 4: Fix Network/Proxy Issues
Configure proxy in gradle.properties:
```properties # HTTP Proxy systemProp.http.proxyHost=proxy.company.com systemProp.http.proxyPort=8080 systemProp.http.proxyUser=proxyuser systemProp.http.proxyPassword=proxypass
# HTTPS Proxy systemProp.https.proxyHost=proxy.company.com systemProp.https.proxyPort=8080 systemProp.https.proxyUser=proxyuser systemProp.https.proxyPassword=proxypass
# Non-proxy hosts systemProp.http.nonProxyHosts=localhost|127.0.0.1|*.internal.com ```
Or in build.gradle:
```groovy plugins { id 'java' }
// Proxy not needed in build.gradle usually // It's better to use gradle.properties ```
Solution 5: Fix Java Version Mismatch
When source/target compatibility is wrong:
```groovy // build.gradle java { toolchain { languageVersion = JavaLanguageVersion.of(17) } }
// Or specify source/target java { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 }
// For Kotlin DSL (build.gradle.kts) java { toolchain { languageVersion.set(JavaLanguageVersion.of(17)) } } ```
If project requires specific Java version:
```bash # Set JAVA_HOME for Gradle export JAVA_HOME=/path/to/jdk-17 ./gradlew build
# Or use Gradle wrapper with specific Java ./gradlew build -Dorg.gradle.java.home=/path/to/jdk-17 ```
Solution 6: Fix Plugin Not Found
Add plugin repository:
```groovy // build.gradle buildscript { repositories { gradlePluginPortal() mavenCentral() google() } dependencies { classpath 'com.example:gradle-plugin:1.0.0' } }
// Or use plugins block plugins { id 'com.example.plugin' version '1.0.0' }
// For plugin portal plugins plugins { id 'java' id 'org.springframework.boot' version '3.2.0' } ```
Solution 7: Fix Multi-Module Dependencies
For multi-module projects:
```groovy // settings.gradle rootProject.name = 'myproject' include 'core', 'web', 'api'
// In web/build.gradle dependencies { // Reference another module implementation project(':core') implementation project(':api')
// External dependencies implementation 'org.springframework.boot:spring-boot-starter-web:3.2.0' } ```
Fix dependency ordering:
```groovy // In api/build.gradle dependencies { // Ensure core is built first implementation project(':core') }
// In web/build.gradle dependencies { // Both api and core will be built implementation project(':api') } ```
Solution 8: Fix Test Failures
When tests fail during build:
```bash # Run tests with more output ./gradlew test --info
# Run specific test ./gradlew test --tests "com.example.MyTest"
# Run tests in specific class ./gradlew test --tests "com.example.*Test"
# Continue build on test failure ./gradlew build --continue ```
Skip tests temporarily:
# Build without running tests
./gradlew build -x testConfigure test in build.gradle:
```groovy test { useJUnitPlatform()
// Show test output testLogging { events "passed", "skipped", "failed" exceptionFormat "full" }
// Fail on first error failFast = true
// Parallel execution maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1 } ```
Solution 9: Fix Kotlin DSL Issues
For build.gradle.kts:
```kotlin // Correct syntax plugins { java id("org.springframework.boot") version "3.2.0" id("io.spring.dependency-management") version "1.1.4" }
group = "com.example" version = "1.0.0"
java { sourceCompatibility = JavaVersion.VERSION_17 }
repositories { mavenCentral() }
dependencies { implementation("org.springframework.boot:spring-boot-starter-web") testImplementation("org.springframework.boot:spring-boot-starter-test") }
tasks.withType<Test> { useJUnitPlatform() } ```
Solution 10: Fix Permission Issues
```bash # Fix file permissions chmod +x gradlew
# Fix ownership chown -R user:user .
# Fix cache permissions chmod -R u+rw ~/.gradle/ ```
Verification
After fixes, verify the build:
```bash # Clean build ./gradlew clean build
# Check dependencies ./gradlew dependencies --configuration runtimeClasspath
# Verify specific task ./gradlew tasks
# Run with verbose output ./gradlew build --info --stacktrace
# Verify artifact ls -la build/libs/ ```
Create a diagnostic script:
```bash #!/bin/bash # diagnose-gradle.sh
echo "=== Gradle Diagnostics ==="
echo -e "\n--- Gradle Version ---" ./gradlew --version
echo -e "\n--- Java Version ---" java -version 2>&1 | head -3
echo -e "\n--- JAVA_HOME ---" echo "JAVA_HOME: $JAVA_HOME"
echo -e "\n--- Gradle Daemon Status ---" ./gradlew --status
echo -e "\n--- Configuration Check ---" ./gradlew properties | grep -E "(version|group|sourceCompatibility)"
echo -e "\n--- Dependency Check ---" ./gradlew dependencies --configuration compileClasspath 2>&1 | head -50
echo -e "\n--- Build Attempt ---" ./gradlew build --stacktrace 2>&1 | tail -30 ```
Common Error Patterns
| Error | Cause | Solution |
|---|---|---|
| Could not find | Missing repository | Add mavenCentral() or other repo |
| Could not resolve | Version doesn't exist | Check correct version |
| Connection refused | Network/proxy issue | Configure proxy settings |
| Permission denied | File permissions | chmod +x gradlew |
| Unsupported class version | Java version mismatch | Update Java or toolchain |
| Plugin not found | Plugin repo missing | Add plugin portal |
| Circular dependency | Module depends on self | Fix project references |
| OutOfMemoryError | Build memory low | Increase Gradle memory |
Increase Gradle Memory
For large projects:
```properties # In gradle.properties org.gradle.jvmargs=-Xmx4g -XX:+HeapDumpOnOutOfMemoryError -XX:MaxMetaspaceSize=512m
# Enable parallel builds org.gradle.parallel=true
# Enable caching org.gradle.caching=true
# Enable configuration cache org.gradle.configuration-cache=true ```
Quick Reference Commands
```bash # Clean everything ./gradlew clean && rm -rf .gradle/ build/
# Refresh dependencies ./gradlew build --refresh-dependencies
# See what went wrong ./gradlew build --stacktrace --info
# Check dependencies ./gradlew dependencies
# Build without tests ./gradlew build -x test
# Force rebuild ./gradlew clean build --no-build-cache ```
When Gradle build fails, run with --stacktrace first, then check dependencies and repositories. Clear caches as a last resort.