Dependency resolution APIs
Dependency resolution in Gradle can largely be thought of as a two-step process. The first being graph resolution, and the second being artifact resolution. The results of these processes are presented through a variety of types, each catering to different use cases:
-
FileCollection - A flat list of files. This is the most common way to access dependency resolution results.
-
ArtifactCollection - Provides access to the metadata for each resolved artifact in addition to the file itself.
-
ResolvedComponentResult and ResolvedVariantResult - Exposes the raw dependency graph without resolving artifacts, describing the relationships between components and variants.
Declaring Configurations
The entrypoint to all dependency resolution APIs is a resolvable Configuration.
The Java plugins primarily use the compileClasspath, and runtimeClasspath configurations to resolve jars for compilation and runtime respectively.
A resolvable configuration is intended for initiating dependency resolution.
The dependencies to be resolved are declared on dependency scope configurations.
The Java plugins use the api, implementation, and runtimeOnly dependency scope configurations, among others, as a source of dependencies to be resolved by the resolvable configurations.
Consider the following example that demonstrates how to declare a set of configurations intended for resolution:
| This example uses incubating APIs. |
val implementation = configurations.dependencyScope("implementation")
val runtimeClasspath = configurations.resolvable("runtimeClasspath") {
extendsFrom(implementation.get())
}
configurations {
dependencyScope("implementation")
resolvable("runtimeClasspath") {
extendsFrom(implementation)
}
}
Dependencies can be declared on the implementation configuration using the dependencies block. See the Declaring Dependencies chapter for more information on the types of dependencies that can be declared, and the various options for customizing dependency declarations.
dependencies {
implementation("com.google.guava:guava:33.2.1-jre")
}
dependencies {
implementation("com.google.guava:guava:33.2.1-jre")
}
Now that we’ve created a dependency scope configuration for declaring dependencies, and a resolvable configuration for resolving those dependencies, we can use Gradle’s dependency resolution APIs to access the results of resolution.
Traversing a dependency graph
In the graph resolution phase, Gradle downloads and examines component metadata (GMM, POM, and IVY XML) for declared and transitive dependencies to construct a dependency graph. This process is described in detail by the Dependency Resolution chapter.
The output of the graph resolution phase is a fully resolved dependency graph, which is used as the input to the artifact resolution phase. The ResolutionResult API provides access to the resolved dependency graph without triggering artifact resolution. This API presents the resolved dependency graph, where each node in the graph is a variant of a component.
Raw access to the dependency graph can be useful for a number of use cases:
-
Visualizing the dependency graph, for example generating a
.dotfile for Graphviz. -
Exposing diagnostics about a given resolution, similar to the
dependenciesordependencyInsighttasks. -
Resolving a subset of the artifacts for a dependency graph when used in conjunction with the
ArtifactViewAPI.
Consider the following function that traverses a dependency graph, starting from the root node. Callbacks are notified for each node and edge in the graph. This function can be used as a base for any use case that requires traversing a dependency graph.
fun traverseGraph(
rootComponent: ResolvedComponentResult,
rootVariant: ResolvedVariantResult,
nodeCallback: (ResolvedVariantResult) -> Unit,
edgeCallback: (ResolvedVariantResult, ResolvedVariantResult) -> Unit
) {
val seen = mutableSetOf<ResolvedVariantResult>(rootVariant)
nodeCallback(rootVariant)
val queue = ArrayDeque(listOf(rootVariant to rootComponent))
while (queue.isNotEmpty()) {
val (variant, component) = queue.removeFirst()
// Traverse this variant's dependencies
component.getDependenciesForVariant(variant).forEach { dependency ->
val resolved = when (dependency) {
is ResolvedDependencyResult -> dependency
is UnresolvedDependencyResult -> throw dependency.failure
else -> throw AssertionError("Unknown dependency type: $dependency")
}
if (!resolved.isConstraint) {
val toVariant = resolved.resolvedVariant
if (seen.add(toVariant)) {
nodeCallback(toVariant)
queue.addLast(toVariant to resolved.selected)
}
edgeCallback(variant, toVariant)
}
}
}
}
void traverseGraph(
ResolvedComponentResult rootComponent,
ResolvedVariantResult rootVariant,
Consumer<ResolvedVariantResult> nodeCallback,
BiConsumer<ResolvedVariantResult, ResolvedVariantResult> edgeCallback
) {
Set<ResolvedVariantResult> seen = new HashSet<>()
seen.add(rootVariant)
nodeCallback(rootVariant)
def queue = new ArrayDeque<Tuple2<ResolvedVariantResult, ResolvedComponentResult>>()
queue.add(new Tuple2(rootVariant, rootComponent))
while (!queue.isEmpty()) {
def entry = queue.removeFirst()
def variant = entry.v1
def component = entry.v2
// Traverse this variant's dependencies
component.getDependenciesForVariant(variant).each { dependency ->
if (dependency instanceof UnresolvedDependencyResult) {
throw dependency.failure
}
if ((!dependency instanceof ResolvedDependencyResult)) {
throw new RuntimeException("Unknown dependency type: $dependency")
}
def resolved = dependency as ResolvedDependencyResult
if (!dependency.constraint) {
def toVariant = resolved.resolvedVariant
if (seen.add(toVariant)) {
nodeCallback(toVariant)
queue.add(new Tuple2(toVariant, resolved.selected))
}
edgeCallback(variant, toVariant)
}
}
}
}
This function starts at the root variant, and performs a breadth-first traversal of the graph.
The ResolutionResult API is lenient, so it is important to check whether a visited edge is unresolved (failed) or resolved.
With this function, the node callback is always called before the edge callback for any given node.
Below, we leverage the above traversal function to transform a dependency graph into a .dot file for visualization:
abstract class GenerateDot : DefaultTask() {
@get:Input
abstract val rootComponent: Property<ResolvedComponentResult>
@get:Input
abstract val rootVariant: Property<ResolvedVariantResult>
@TaskAction
fun traverse() {
println("digraph {")
traverseGraph(
rootComponent.get(),
rootVariant.get(),
{ node -> println(" ${toNodeId(node)} [shape=box]") },
{ from, to -> println(" ${toNodeId(from)} -> ${toNodeId(to)}") }
)
println("}")
}
fun toNodeId(variant: ResolvedVariantResult): String {
return "\"${variant.owner.displayName}:${variant.displayName}\""
}
}
abstract class GenerateDot extends DefaultTask {
@Input
abstract Property<ResolvedComponentResult> getRootComponent()
@Input
abstract Property<ResolvedVariantResult> getRootVariant()
@TaskAction
void traverse() {
println("digraph {")
traverseGraph(
rootComponent.get(),
rootVariant.get(),
node -> { println(" ${toNodeId(node)} [shape=box]") },
(from, to) -> { println(" ${toNodeId(from)} -> ${toNodeId(to)}") }
)
println("}")
}
String toNodeId(ResolvedVariantResult variant) {
return "\"${variant.owner.displayName}:${variant.displayName}\""
}
}
A proper implementation would not use println but would write to an output file. For more details on declaring task inputs and outputs, see the Writing Tasks section.
|
When we register the task, we use the ResolutionResult API to access the root component and root variant of the runtimeClasspath Configuration.
| This example uses incubating APIs. |
tasks.register<GenerateDot>("generateDot") {
rootComponent = runtimeClasspath.flatMap {
it.incoming.resolutionResult.rootComponent
}
rootVariant = runtimeClasspath.flatMap {
it.incoming.resolutionResult.rootVariant
}
}
tasks.register("generateDot", GenerateDot) {
rootComponent = configurations.runtimeClasspath.incoming.resolutionResult.rootComponent
rootVariant = configurations.runtimeClasspath.incoming.resolutionResult.rootVariant
}
Running this task, we get the following output:
digraph {
"root project ::runtimeClasspath" [shape=box]
"com.google.guava:guava:33.2.1-jre:jreRuntimeElements" [shape=box]
"root project ::runtimeClasspath" -> "com.google.guava:guava:33.2.1-jre:jreRuntimeElements"
"com.google.guava:failureaccess:1.0.2:runtime" [shape=box]
"com.google.guava:guava:33.2.1-jre:jreRuntimeElements" -> "com.google.guava:failureaccess:1.0.2:runtime"
"com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava:runtime" [shape=box]
"com.google.guava:guava:33.2.1-jre:jreRuntimeElements" -> "com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava:runtime"
"com.google.code.findbugs:jsr305:3.0.2:runtime" [shape=box]
"com.google.guava:guava:33.2.1-jre:jreRuntimeElements" -> "com.google.code.findbugs:jsr305:3.0.2:runtime"
"org.checkerframework:checker-qual:3.42.0:runtimeElements" [shape=box]
"com.google.guava:guava:33.2.1-jre:jreRuntimeElements" -> "org.checkerframework:checker-qual:3.42.0:runtimeElements"
"com.google.errorprone:error_prone_annotations:2.26.1:runtime" [shape=box]
"com.google.guava:guava:33.2.1-jre:jreRuntimeElements" -> "com.google.errorprone:error_prone_annotations:2.26.1:runtime"
}
Compare this to the output of the dependencies task:
runtimeClasspath
\--- com.google.guava:guava:33.2.1-jre
+--- com.google.guava:failureaccess:1.0.2
+--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
+--- com.google.code.findbugs:jsr305:3.0.2
+--- org.checkerframework:checker-qual:3.42.0
\--- com.google.errorprone:error_prone_annotations:2.26.1
Notice how the graph is the same for both representations.
Artifact resolution
After constructing a dependency graph, Gradle can perform artifact resolution on the resolved graph.
Gradle APIs can be used to influence the process of artifact selection — the mapping of a graph to a set of artifacts.
Gradle can then expose the results of artifact selection as an ArtifactCollection.
More commonly, however, the results are exposed as a FileCollection, which is a flat list of files that are downloaded in parallel.
Artifact selection operates on the dependency graph on a node-by-node basis.
Each node in the graph may expose multiple sets of artifacts, but only one of those sets may be selected.
For example, the runtimeElements variant of the Java plugins exposes a jar, classes, and resources artifact set.
These three artifact sets represent the same distributable, but in different forms.
For each node (variant) in a graph, Gradle performs attribute matching over each set of artifacts exposed by that node to determine the best artifact set. If no artifact sets match the requested attributes, Gradle will attempt to construct an artifact transform chain to satisfy the request.
For more details on the attribute matching process, see the attribute matching section.
Implicit artifact selection
By default, the attributes used for artifact selection are the same as those used for variant selection during graph resolution.
These attributes are specified by the Configuration#getAttributes() property.
To perform artifact selection (and implicitly, graph resolution) using these default attributes, use the FileCollection and ArtifactCollection APIs.
Files can also be accessed from the configuration’s ResolvedConfiguration, LenientConfiguration, ResolvedArtifact and ResolvedDependency APIs.
However, these APIs are in maintenance mode and are discouraged for use in new development.
These APIs perform artifact selection using the default attributes.
|
Resolving files
To resolve files, we first define a task that accepts a ConfigurableFileCollection as input:
abstract class ResolveFiles : DefaultTask() {
@get:InputFiles
abstract val files: ConfigurableFileCollection
@TaskAction
fun print() {
files.forEach {
println(it.name)
}
}
}
abstract class ResolveFiles extends DefaultTask {
@InputFiles
abstract ConfigurableFileCollection getFiles()
@TaskAction
void print() {
files.each {
println(it.name)
}
}
}
Then, we can wire up a resolvable configuration’s files to the task’s input.
The Configuration directly implements FileCollection and can be wired directly.
Alternatively, wiring through Configuration#getIncoming() is a more explicit approach:
tasks.register<ResolveFiles>("resolveConfiguration") {
files.from(configurations.runtimeClasspath)
}
tasks.register<ResolveFiles>("resolveIncomingFiles") {
files.from(configurations.runtimeClasspath.map { it.incoming.files })
}
tasks.register("resolveConfiguration", ResolveFiles) {
files.from(configurations.runtimeClasspath)
}
tasks.register("resolveIncomingFiles", ResolveFiles) {
files.from(configurations.runtimeClasspath.incoming.files)
}
Running both of these tasks, we can see the output is identical:
> Task :resolveConfiguration junit-platform-commons-1.11.0.jar junit-jupiter-api-5.11.0.jar opentest4j-1.3.0.jar > Task :resolveIncomingFiles junit-platform-commons-1.11.0.jar junit-jupiter-api-5.11.0.jar opentest4j-1.3.0.jar
Resolving artifacts
Instead of consuming the files directly from the implicit artifact selection process, we can consume the artifacts, which contain both the files and the metadata.
This process is slightly more complicated, as in order to maintain Configuration Cache compatibility, we need to split the fields of ResolvedArtifactResult into two task inputs:
data class ArtifactDetails(
val id: ComponentArtifactIdentifier,
val variant: ResolvedVariantResult
)
abstract class ResolveArtifacts : DefaultTask() {
@get:Input
abstract val details: ListProperty<ArtifactDetails>
@get:InputFiles
abstract val files: ListProperty<File>
fun from(artifacts: Provider<Set<ResolvedArtifactResult>>) {
details.set(artifacts.map {
it.map { artifact -> ArtifactDetails(artifact.id, artifact.variant) }
})
files.set(artifacts.map {
it.map { artifact -> artifact.file }
})
}
@TaskAction
fun print() {
assert(details.get().size == files.get().size)
details.get().zip(files.get()).forEach { (details, file) ->
println("${details.variant.displayName}:${file.name}")
}
}
}
class ArtifactDetails {
ComponentArtifactIdentifier id
ResolvedVariantResult variant
ArtifactDetails(ComponentArtifactIdentifier id, ResolvedVariantResult variant) {
this.id = id
this.variant = variant
}
}
abstract class ResolveArtifacts extends DefaultTask {
@Input
abstract ListProperty<ArtifactDetails> getDetails()
@InputFiles
abstract ListProperty<File> getFiles()
void from(Provider<Set<ResolvedArtifactResult>> artifacts) {
details.set(artifacts.map {
it.collect { artifact -> new ArtifactDetails(artifact.id, artifact.variant) }
})
files.set(artifacts.map {
it.collect { artifact -> artifact.file }
})
}
@TaskAction
void print() {
List<ArtifactDetails> allDetails = details.get()
List<File> allFiles = files.get()
assert allDetails.size() == allFiles.size()
for (int i = 0; i < allDetails.size(); i++) {
def details = allDetails.get(i)
def file = allFiles.get(i)
println("${details.variant.displayName}:${file.name}")
}
}
}
This task is initialized similarly to the file resolution task:
tasks.register<ResolveArtifacts>("resolveIncomingArtifacts") {
from(configurations.runtimeClasspath.flatMap { it.incoming.artifacts.resolvedArtifacts })
}
tasks.register("resolveIncomingArtifacts", ResolveArtifacts) {
from(configurations.runtimeClasspath.incoming.artifacts.resolvedArtifacts)
}
Running this task, we can see that file metadata is included in the output:
org.junit.platform:junit-platform-commons:1.11.0 variant runtimeElements:junit-platform-commons-1.11.0.jar org.junit.jupiter:junit-jupiter-api:5.11.0 variant runtimeElements:junit-jupiter-api-5.11.0.jar org.opentest4j:opentest4j:1.3.0 variant runtimeElements:opentest4j-1.3.0.jar
Customizing artifact selection
In some cases, it is desirable to customize the selection process.
The ArtifactView API is the primary mechanism for influencing artifact selection in Gradle.
An ArtifactView can:
-
Trigger artifact transforms
-
Select alternative variants, such as sources or javadoc, for an entire resolution
-
Perform lenient artifact selection and resolution
-
Filter selected artifacts
The ArtifactView can produce results as both a FileCollection and an ArtifactCollection.
The below examples will only demonstrate using a FileCollection as the output.
|
Triggering artifact transforms
An ArtifactView can be used to trigger artifact selection using attributes different from those used to resolve the graph.
For each node in the graph, artifact selection is performed for that node. Most commonly, this API is used to request attributes that are not present on any artifact set from the variant that artifacts are being selected from. When Gradle cannot find a matching artifact set from the node in question, it will attempt to satisfy the request by transforming the available artifact sets using the artifact transforms registered on the project.
Below, we use the unzip example from the artifact transforms chapter to demonstrate how to use the ArtifactView API to request attributes that trigger a transform:
tasks.register<ResolveFiles>("resolveTransformedFiles") {
files.from(configurations.runtimeClasspath.map {
it.incoming.artifactView {
attributes {
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.CLASSES_AND_RESOURCES))
attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE)
}
}.files
})
}
tasks.register("resolveTransformedFiles", ResolveFiles) {
files.from(configurations.runtimeClasspath.incoming.artifactView {
attributes {
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements, LibraryElements.CLASSES_AND_RESOURCES))
attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE)
}
}.files)
}
Gradle performs artifact selection using the graph resolution attributes specified on the configuration, concatenated with the attributes specified in the attributes block of the ArtifactView.
The task output shows that the artifacts have been transformed:
junit-platform-commons-1.11.0.jar-unzipped junit-jupiter-api-5.11.0.jar-unzipped opentest4j-1.3.0.jar-unzipped
Performing variant reselection
Standard artifact selection can only select between and transform artifact sets exposed by the node under selection. However, in some cases, it may be desirable to select artifacts from a variant parallel to the graph node being selected.
Consider the example component structure below, describing a typical local Java library with sources and javadoc:
variant 'apiElements'
artifact set 'jar'
artifact set 'classes'
artifact set 'resources'
variant 'runtimeElements'
artifact set 'jar'
artifact set 'classes'
artifact set 'resources'
variant 'javadocElements'
artifact set 'jar'
variant 'sourcesElements'
artifact set 'jar'
Resolving a Java runtime classpath will select the runtimeElements variant from the above example component.
During standard artifact selection, Gradle will select solely from the artifact sets under runtimeElements.
However, it is common to want to select all sources or all javadoc for every node in the graph. Consider the following example which selects all sources for a given runtime classpath:
| This example uses incubating APIs. |
tasks.register<ResolveFiles>("resolveSources") {
files.from(configurations.runtimeClasspath.map {
it.incoming.artifactView {
withVariantReselection()
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME));
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION));
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL));
attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.SOURCES));
}
}.files
})
}
tasks.register("resolveSources", ResolveFiles) {
files.from(configurations.runtimeClasspath.incoming.artifactView {
withVariantReselection()
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME));
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION));
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling, Bundling.EXTERNAL));
attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, DocsType.SOURCES));
}
}.files)
}
Using the ArtifactView#withVariantReselection() API, Gradle will optionally perform graph variant selection again before performing artifact selection on the new selected variant.
When Gradle selects artifacts for the runtimeElements node, it will use the attributes specified on the ArtifactView to reselect the graph variant, thus selecting the sourcesElements variant instead.
Then, traditional artifact selection will be performed on the sourcesElements variant to select the jar artifact set.
As a result, the sources jar is resolved for each node:
junit-platform-commons-1.11.0-sources.jar junit-jupiter-api-5.11.0-sources.jar opentest4j-1.3.0-sources.jar
When this API is used, the attributes used for variant reselection are specified solely by the ArtifactView#getAttributes() method.
The graph resolution attributes specified on the configuration are completely ignored during variant reselection.
Performing lenient artifact resolution
The ArtifactView API can also be used to perform lenient artifact resolution.
This allows artifact resolution to be performed on a graph that contains failures — for example when a requested module was not found, the requested module version did not exist, or a conflict was not resolved.
Furthermore, lenient artifact resolution can be used to resolve artifacts when the graph was successfully resolved, but the corresponding artifacts could not be downloaded.
Consider the following example, where some dependencies may not exist:
dependencies {
implementation("does:not:exist")
implementation("org.junit.jupiter:junit-jupiter-api:5.11.0")
}
dependencies {
implementation("does:not:exist")
implementation("org.junit.jupiter:junit-jupiter-api:5.11.0")
}
Lenient resolution is performed by using the ArtifactView#lenient() method.
tasks.register<ResolveFiles>("resolveLenient") {
files.from(configurations.runtimeClasspath.map {
it.incoming.artifactView {
isLenient = true
}.files
})
}
tasks.register("resolveLenient", ResolveFiles) {
files.from(configurations.runtimeClasspath.incoming.artifactView {
lenient = true
}.files)
}
We can see that the task succeeds with the failing artifact omitted:
> Task :resolveLenient junit-platform-commons-1.11.0.jar junit-jupiter-api-5.11.0.jar opentest4j-1.3.0.jar BUILD SUCCESSFUL in 0s
Filtering artifacts
The ArtifactView API can be used to filter specific artifacts from the resulting FileCollection or ArtifactCollection.
ArtifactViews allow results to be filtered on a per-component basis.
Using the ArtifactView#componentFilter(Action) method, artifacts from certain components may be filtered from the result.
The action is passed the ComponentIdentifier of the component that owns the variant that artifacts are being selected for.
Consider the following example, where we have one project dependency and one external dependency:
dependencies {
implementation(project(":other"))
implementation("org.junit.jupiter:junit-jupiter-api:5.11.0")
}
dependencies {
implementation(project(":other"))
implementation("org.junit.jupiter:junit-jupiter-api:5.11.0")
}
Using the componentFilter method, we can specify filters that select only artifacts of a certain type:
tasks.register<ResolveFiles>("resolveProjects") {
files.from(configurations.runtimeClasspath.map {
it.incoming.artifactView {
componentFilter {
it is ProjectComponentIdentifier
}
}.files
})
}
tasks.register<ResolveFiles>("resolveModules") {
files.from(configurations.runtimeClasspath.map {
it.incoming.artifactView {
componentFilter {
it is ModuleComponentIdentifier
}
}.files
})
}
tasks.register("resolveProjects", ResolveFiles) {
files.from(configurations.runtimeClasspath.incoming.artifactView {
componentFilter {
it instanceof ProjectComponentIdentifier
}
}.files)
}
tasks.register("resolveModules", ResolveFiles) {
files.from(configurations.runtimeClasspath.incoming.artifactView {
componentFilter {
it instanceof ModuleComponentIdentifier
}
}.files)
}
Notice how we resolve project dependencies and module dependencies separately.
> Task :resolveProjects other.jar > Task :resolveModules junit-platform-commons-1.11.0.jar junit-jupiter-api-5.11.0.jar opentest4j-1.3.0.jar