Download unselected dependencies

I’m now trying to develop a gradle plugin which can help analyze dependency problems in Gradle projects. However, i have encountered a problem. I need to download the artifacts of a certain depenendecy which is left unselected because there are dependencies of the same component.

The problem is i need to download and analyze the artifacts of such dependencies to carry on with my project. Is there anything like the ArtifactResolver in Maven , which I provide the GAV coordinates, and it helps me download the artifacts. It would be much of a help to carry me through this problem.

What you are after is an ArtifactResolutionQuery.

1 Like

Thansk for your help. But i tried it and found that it used the component id which was actually selected in the resolution process instead of the requested component. Even if i use the ForModule method, it returns with artifactResults of size 0.

Then you probably did something wrong :wink:

I tried to use the api in several ways but they either fail or give the selected version rather than the requested one. There are little example on the internet on how to use it, it’s likely i did something wrong.

So i had to change the way to do it into imitating how it’s done in build.gradle. I add a new configuration and add dependencies to it. It seems to work, but i wonder it has any negative consequences.

As I know it works as you intend to, you should provide an MCVE with your failed try with forModule, then you can probably be advised on what might be the error. :wink:

I added dependencies block in my build.gradle file like this with two conflicting versions.

dependencies {



And I implement a task like this

task resolveCompileSources {
    doLast {

        def result = dependencies.createArtifactResolutionQuery()
                .forModule("org.apache.commons", "commons-collections4", "4.2")

I expect to get the result of version 4.2 including the jarfile path etc. But instead, the result contains a ComponentResult with no ArtifactResult.

Whoops, I’m sorry, I was wrong actually.
You cannot get the code artifact through the ArtifactResolutionQuery.
You can only get the sources, javadoc, pom, and ivy files.
You cannot even get the Gradle Module Metadata, strange.

That your artifacts are empty is because you didn’t request any. JvmLibrary is the component and further arguments to withArtifacts would define which artifacts to get.

So yeah, normal resolution is the way to go.
You can either create normal configurations of this, or you can create “detached configurations”, depending on the concrete use-case.

And also ArtifactResolutionQuery is going to be replaced by “something”: duck-typing dependent example in JavaDoc of `ArtifactResolutionQuery` · Issue #19227 · gradle/gradle · GitHub

Thanks for your help! I mistake the JvmLibrary with the artifact that i want.

I notice that detached configurations should be created with dependencies, but the dependencies create need to specify a config name. So which of the two should be created first? I tried to use something like

        Configuration config = project.getConfigurations().detachedConfiguration();
        project.getDependencies().add(config.getName(), displayName);

But it failed as it cannot get the name of the detachedConfiguration.

The configuration is detached, so you don’t add dependencies via that dependencies.
You add them directly to that detached configuration.