Dependency Configurations & Conflicts
Maven has one list of dependencies with a scope on each. Gradle has configurations: named buckets of dependencies, wired together. Once you see the three roles a configuration can play, the dependencies report and every error message about variants start to make sense.
Three roles
| Role | Examples (from the java/java-library plugins) |
Purpose |
|---|---|---|
| Declarable (you add to it) | implementation, api, compileOnly, runtimeOnly, testImplementation, testRuntimeOnly, annotationProcessor |
where your build script declares dependencies |
| Resolvable (Gradle resolves it into files) | compileClasspath, runtimeClasspath, testCompileClasspath, testRuntimeClasspath |
the actual classpaths of tasks; they extend declarable ones |
| Consumable (others resolve against it) | apiElements, runtimeElements |
what this project offers to other projects and to publications |
So compileClasspath = compileOnly + implementation + api, and runtimeClasspath = runtimeOnly + implementation + api. When app depends on project(":lib"), its compileClasspath picks lib's apiElements (only api dependencies) and its runtimeClasspath picks runtimeElements (both). That's the mechanism behind api vs implementation.
Conflict resolution: highest wins
When two requests for the same module meet, Gradle picks the highest version, wherever it sits in the graph. Here app asks for Guava 33.5.0-jre directly, and lib (a project app depends on) asks for 33.7.2-jre:
mkdir -p d/lib d/app && cd d
cat > settings.gradle.kts <<'KTS'
rootProject.name = "d"
dependencyResolutionManagement { repositories { mavenCentral() } }
include("lib", "app")
KTS
cat > lib/build.gradle.kts <<'KTS'
plugins { `java-library` }
dependencies { api("com.google.guava:guava:33.7.2-jre") }
KTS
cat > app/build.gradle.kts <<'KTS'
plugins { java }
dependencies {
implementation("com.google.guava:guava:33.5.0-jre")
implementation(project(":lib"))
}
KTS
gradle -q :app:dependencies --configuration runtimeClasspath | grep -- '--- '
gradle -q :app:dependencyInsight --dependency com.google.guava:guava --configuration runtimeClasspath \
| grep -A1 -E 'Selection reasons|^com.google.guava:guava:33.5.0-jre ->'+--- com.google.guava:guava:33.5.0-jre -> 33.7.2-jre
| +--- com.google.guava:failureaccess:1.0.3
| +--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
| +--- org.jspecify:jspecify:1.0.1
| +--- com.google.errorprone:error_prone_annotations:2.50.0
| \--- com.google.j2objc:j2objc-annotations:3.1
\--- project ':lib'
\--- com.google.guava:guava:33.7.2-jre (*)
Selection reasons:
- By conflict resolution: between versions 33.7.2-jre and 33.5.0-jre
--
com.google.guava:guava:33.5.0-jre -> 33.7.2-jre
\--- runtimeClasspath
In the tree, 33.5.0-jre -> 33.7.2-jre means "requested 33.5.0-jre, got 33.7.2-jre", and (*) marks a subtree already shown. dependencyInsight answers why for one module: by conflict resolution.
Maven would have done the opposite: the direct declaration is nearest, so Maven uses 33.5.0-jre. Neither rule is "right". Highest-wins rarely produces a NoSuchMethodError (newer versions usually keep old methods), but it can upgrade a library further than you tested, silently, because a dependency was updated.
Taking control: constraints, strict versions, platforms
Gradle gives you more precise tools than Maven's single dependencyManagement:
| Tool | Written as | Effect |
|---|---|---|
| a constraint | constraints { implementation("g:a:1.2") } |
if g:a appears anywhere, it's at least 1.2; adds nothing by itself |
| a strict version | implementation("g:a") { version { strictly("1.2") } } |
exactly 1.2, overriding (even downgrading) other requests |
| a platform | implementation(platform("g:bom:1.0")) |
imports a BOM's versions as constraints |
enforcedPlatform(...) |
a platform whose versions are strict | |
failOnVersionConflict() |
on a configuration's resolutionStrategy |
any conflict fails the build (Maven enforcer's dependencyConvergence) |
mkdir -p d/lib d/app/src/main/java/app && cd d
cat > settings.gradle.kts <<'KTS'
rootProject.name = "d"
dependencyResolutionManagement { repositories { mavenCentral() } }
include("lib", "app")
KTS
cat > lib/build.gradle.kts <<'KTS'
plugins { `java-library` }
dependencies { api("com.google.guava:guava:33.7.2-jre") }
KTS
cat > app/build.gradle.kts <<'KTS'
plugins { java }
dependencies {
implementation(project(":lib"))
implementation("com.google.guava:guava") { version { strictly("33.5.0-jre") } }
implementation(platform("com.fasterxml.jackson:jackson-bom:2.22.4"))
implementation("com.fasterxml.jackson.core:jackson-databind")
}
KTS
cat > app/src/main/java/app/Main.java <<'SRC'
package app;
public class Main {
public static void main(String[] args) throws Exception {
var json = new com.fasterxml.jackson.databind.ObjectMapper()
.writeValueAsString(com.google.common.collect.ImmutableMap.of("ok", true));
System.out.println(json);
}
}
SRC
gradle -q :app:dependencies --configuration runtimeClasspath | grep -- '--- '+--- project ':lib'
| \--- com.google.guava:guava:33.7.2-jre -> 33.5.0-jre
| +--- com.google.guava:failureaccess:1.0.3
| +--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
| +--- org.jspecify:jspecify:1.0.0
| +--- com.google.errorprone:error_prone_annotations:2.41.0
| \--- com.google.j2objc:j2objc-annotations:3.1
+--- com.google.guava:guava:{strictly 33.5.0-jre} -> 33.5.0-jre (*)
+--- com.fasterxml.jackson:jackson-bom:2.22.4
| +--- com.fasterxml.jackson.core:jackson-databind:2.22.4 (c)
| +--- com.fasterxml.jackson.core:jackson-annotations:2.22 (c)
| \--- com.fasterxml.jackson.core:jackson-core:2.22.4 (c)
\--- com.fasterxml.jackson.core:jackson-databind -> 2.22.4
+--- com.fasterxml.jackson.core:jackson-annotations:2.22
+--- com.fasterxml.jackson.core:jackson-core:2.22.4
| \--- com.fasterxml.jackson:jackson-bom:2.22.4 (*)
\--- com.fasterxml.jackson:jackson-bom:2.22.4 (*)
{strictly 33.5.0-jre}won overlib's request (33.7.2-jre -> 33.5.0-jre), and Guava's own dependencies followed it down (jspecify 1.0.0, error_prone 2.41.0). Use strict versions sparingly: you've overridden whatlib's authors tested with.jackson-databindhas no version in the script; the arrow-> 2.22.4shows the version came from the platform. The platform appears in the tree as a dependency, and its entries are marked(c): constraints, not dependencies. Jackson's own modules also depend on the BOM, so the versions stay aligned even for modules you add later.
Tip:
gradle dependenciesprints every configuration of a project, which is long; always add--configuration runtimeClasspath(orcompileClasspath).gradle buildEnvironmentshows the same tree for the build's own classpath (plugins), which is where plugin version conflicts hide.
Test yourself
A project requests lib:1.0 directly and gets lib:2.0 transitively. What does Gradle use by default?
- 1.0, the direct declaration
- 2.0, the highest version
- Both
- The build fails
Which configuration does Gradle actually resolve to compile src/main/java?
- implementation
- compileClasspath
- apiElements
- compileOnly
How do you see why Gradle chose a particular version of a module?
- gradle tasks
- gradle dependencyInsight --dependency <module> --configuration <name>
- gradle projects
- gradle --dry-run