Getting the file of a resolved dependency


(Jonathan Gafner) #1

Hi

Can you please suggest on a good way to fetch the file of a resolved dependency added to a configuration ?

We tried this :

plugins {
id 'java'
}

configurations { myConfig }

Dependency dep1
Dependency dep2

dependencies {
    dep1  = add("myConfig", [group: 'commons-collections', name: 'commons-collections', version: '3.2'])
    dep2  = add("myConfig", [group: 'commons-collections', name: 'commons-collections', version: '3.1'])
}

project.tasks.register("fetch") { Task task ->
    task.doLast {
        Configuration configuration = configurations.myConfig
        logger.quiet "dep1 files:\n${configuration.files(dep1).join("\n")}"
        logger.quiet "dep2 files:\n${configuration.files(dep2).join("\n")}"
    }
}

=========================================================
but when asking for deps2 that was replaced by deps1 (version resolution) we do not get the file of the resolved dependency.

Since we need to copy the file resolved we wanted to know what is the right way after adding a dependency to get its file or the file it was resolved to having in hand only the dependency and the configuration.


(Scott Babcock) #2

In the Report Portal agent-java-junit project, I used this approach to get the file of a JAR that’s used as a Java agent:

ext {
    junitFoundation = configurations.compile.resolvedConfiguration.resolvedArtifacts.find { it.name == 'junit-foundation' }
}
test.doFirst {
    jvmArgs "-javaagent:${junitFoundation.file}"
}

Note that the use of resolvedConfiguration essentially freezes the target configuration, so all necessary elements need to be in place prior to performing this operation. For me, this means placing these two blocks near the end of my build script.