Given a org.gradle.api.artifacts.result.ResolvedDependencyResult
, is there a way to access the artifact(s) represented by this dependency result?
You can use the selected component id in an artifact resolution query.
http://gradle.org/docs/current/dsl/org.gradle.api.artifacts.query.ArtifactResolutionQuery.html
So something like the following?
ResolvedComponentResult result = ...;
project.dependencies.createArtifactResolutionQuery()
.forComponents( result.id )
.execute()
I don’t really understand what to do with the withArtifacts
method
This API would allow you to get additional artifacts like the JavaDoc or Sources jars, or metadata like ivy.xml or pom.xml files. Simply, you ask for a superclass of Artifact
.
https://gradle.org/docs/current/javadoc/org/gradle/api/component/Artifact.html
That said, if you want the default artifact, (the JAR/WAR/etc itself) then you could do something like.
configurations.compile.resolvedConfiguration.resolvedArtifacts.each {
if (it.moduleVersion.id == resolvedDependencyResult.moduleVersion) {
println it.file
}
}
Can I make a suggestion moving forward? IMO a method to bridge from a ResolvedComponentResult to its ResolveArtifact just makes too much sense… ResolvedComponentResult#getResolvedArtifact()
or even ResolvedComponentResult#getResolvedArtifacts()
if there can be more than one.
A follow-up question…
configurations.compile.resolvedConfiguration.resolvedArtifacts
does not seem to properly account for project() deps. Am I missing something?
– UPDATE
I found the “problem”. project.configurations.runtime.incoming.resolutionResult
contains the project itself, whereas project.configurations.runtime.resolvedConfiguration.resolvedArtifacts
does not.