Configurations and dependency resolution
What you will learn
Configurations describe how dependencies are declared, resolved and exposed. A project dependency connects the build graph and the consumer classpath; version catalogs centralize requested coordinates, not resolved versions.
- Choose implementation versus api and runtimeOnly versus compileOnly.
- Trace project dependencies and inspect a resolved configuration.
- Distinguish version catalogs, platforms, dependency locks and integrity verification.
Prerequisite: Java packages, classpath and basic terminal compilation. Gradle CLI examples are static and not executed in this batch. The project is local and fictional; use only reviewed source and approved cached dependencies.
Core library build
plugins { `java-library` }java { toolchain { languageVersion.set(JavaLanguageVersion.of(21)) }}tasks.withType<JavaCompile>().configureEach { options.release.set(21) options.encoding = "UTF-8" options.compilerArgs.add("-proc:none")}tasks.withType<Jar>().configureEach { isPreserveFileTimestamps = false isReproducibleFileOrder = true}Complete core script; Java library plugin, explicit release, disabled annotation processing and reproducible archive settings. Gradle execution untested.
Classpath and variants
With java-library, api exposes a dependency required by the public API to consumer compilation; implementation hides it from consumer compilation but may supply runtime dependencies. compileOnly is for compile-time requirements not supplied at runtime by this dependency declaration; runtimeOnly is not on main compileClasspath; testImplementation is for tests. A declaration configuration is not necessarily resolvable. Inspect compileClasspath or runtimeClasspath rather than resolving implementation. Plugins select variants by attributes. Gradle generally selects the highest candidate version for a version conflict unless constraints, rich versions, rules or locks change it; do not assume Maven nearest-definition mediation.
Catalogs and platforms
A catalog alias expands to dependency coordinates. A version declared there is a request; it does not force the final version or lock transitive resolution. A platform or imported Maven BOM contributes constraints, not all the libraries themselves. A versionless declaration needs a compatible constraint or other version source. Dependency locking records selected versions for configurations actually resolved; regenerate deliberately, review lock diffs and commit. Verification metadata records trusted expected checksums or signatures; a lock is not an integrity check. Do not generate verification metadata from unknown artifacts and automatically trust it.
A local project graph
The app directly declares implementation(project(":core")), making the library visible to app compilation and runtime; core does not depend on app. Both use a Java 21 toolchain and release. No external repositories or dependencies are configured in the runnable fixture. For separate external examples use approved repositories centrally, distinguish plugin resolution from library resolution and stop when an approved cache lacks an artifact.
Application build
plugins { application }dependencies { implementation(project(":core")) }application { mainClass.set("example.bookstore.Main") }java { toolchain { languageVersion.set(JavaLanguageVersion.of(21)) }}tasks.withType<JavaCompile>().configureEach { options.release.set(21) options.encoding = "UTF-8" options.compilerArgs.add("-proc:none")}tasks.withType<Jar>().configureEach { isPreserveFileTimestamps = false isReproducibleFileOrder = true}val verifyReceipt = tasks.register<JavaExec>("verifyReceipt") { classpath = sourceSets["main"].runtimeClasspath mainClass.set("example.bookstore.Main") args("--verify")}tasks.named("check") { dependsOn(verifyReceipt) }Complete app script. Predicted :app:run prints Total cents: 750. The Java sources and resource are separately compiled/tested with JDK tools.
Catalog aliases
# Illustrative gradle/libs.versions.toml; fictional artifacts, not fetched.[versions]format = "1.2.0"[libraries]book-format = { module = "example.bytemind:book-format", version.ref = "format" }book-bom = { module = "example.bytemind:book-bom", version = "2.0.0" }book-parser = { module = "example.bytemind:book-parser" }Illustrative fictional coordinates, not included in the live project. book-format becomes libs.book.format; book-parser gets its version from a platform if available.
Platform and locking
// Fragment: fictional dependencies in an approved local repository only.dependencies { implementation(libs.book.format) implementation(platform(libs.book.bom)) implementation(libs.book.parser)}dependencyLocking { lockAllConfigurations() }Illustrative only; requires approved local repository and matching catalog. The platform supplies version constraints; no artifacts were resolved.
Inspect the graph
# Conditional commands: trusted installed Gradle 9.2.0; no wrapper binaries bundled.gradle --offline :app:dependencies --configuration runtimeClasspathgradle --offline :app:dependencyInsight --dependency core --configuration runtimeClasspath# For a reviewed external-dependency build: resolve selected configurations and write locks.gradle --offline :app:dependencies --write-locksConditional commands; compare runtimeClasspath and compileClasspath in a trusted build. dependencyInsight explains selection reasons. Write locks only in a reviewed change; no commands executed here.
Common mistakes
Do not treat api as a synonym for implementation, resolve implementation directly, or assume catalog aliases lock transitive versions. A BOM does not automatically add every managed library. Avoid dynamic versions and unreviewed repository additions.
Practice
Classify dependencies for a library exposing a parser type in its public signature, a private formatter, an annotation needed only at compile time, a runtime driver and a test helper. Explain how a BOM, catalog, lock and checksum differ.
Use the manual practice hints; explain your reasoning before consulting the private review solution.
Official reading: library · catalog · platform · locking · verification.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Configurations and dependency resolution — knowledge check
Gradle model and build engineering knowledge check; source-reviewed, not Gradle-executed.
Which configuration exposes a public API dependency to consumers?
What does a version catalog guarantee by itself?
What does an imported platform supply?
Which configuration should be inspected for runtime artifacts?
What does dependency verification check?