Gathering project dependencies into folder with Gradle 7

I need to collect all the dependencies required by a project into a directory. How can that be done with Gradle 7?

This question came up before for older versions of Gradle where the solution is to use:

task copyToLib(type: Copy) {
    into "$buildDir/output/lib"
    from configurations.runtime
}

I’ve been using an adapted version of that for years using runtimeOnly:

task copyToLib(type: Copy) {
    into "$buildDir/output/lib"
    from configurations.runtimeOnly
}

Now after upgrading to Gradle 7.4.2 from Gradle 6.X, I get this error:

> Resolving dependency configuration 'runtimeOnly' is not allowed as it is defined as 'canBeResolved=false'.
  Instead, a resolvable ('canBeResolved=true') dependency configuration that extends 'runtimeOnly' should be resolved.

There’s actually a hint on how to solve this, but I’m not sure how to create that resolvable configuration that extends runtimeOnly. Does somebody know how to do that?

I think I found out myself how to do it by reading this guide about configuration containers.

Looks like I can simply add a configuration called runtime that extends from both compileClasspath and runtimeOnly:

configurations {
    runtime.extendsFrom(compileClasspath, runtimeOnly)
}

and then using that configuration copies all required dependencies:

task copyToLib(type: Copy) {
    into "$buildDir/output/lib"
    from configurations.runtime
}

Edit:

I noticed that I probably just confused runtimeOnly and runtimeClasspath. This also works without creating an additional custom configuration:

task copyToLib(type: Copy) {
    into "$buildDir/output/lib"
    from configurations.runtimeClasspath
}
1 Like