Copy Gradle dependencies from another subproject without deprecation warning

Viewed 491

In a Gradle project I have multiple sub-projects. I need to copy the dependencies from the configuration of one sub-project to that of another. I can achieve this using the following, adapted from this answer here:

task copyDependencies(type: Copy) {
    from project(":sub").configurations.compile
    into "${buildDir}/libraries"
}

Unfortunately, with Gradle 5.1, this generates the following deprecation warning (truncated for brevity):

The configuration :sub:compile was resolved without accessing the project in a safe manner. ... This behaviour has been deprecated and is scheduled to be removed in Gradle 6.0.

What's the best way to do this now for Gradle 5.1 and later?

1 Answers

The warning appeared as reaching across project boundaries can cause issues when the build runs in parallel.

The proper way of doing this is to let Gradle know that the project declaring the copyDependencies task will need the project :sub's dependencies.

You can achieve this by doing the following:

configurations {
    dependenciesToCopy
}
dependencies {
    dependenciesToCopy project(':sub')
}
task copyDependencies(type: Copy) {
    from configurations.dependenciesToCopy
    into "${buildDir}/libraries"
}

This will cause Gradle to properly create an execution dependency between your project and the resolution of the dependencies of the sub project.

Relevant discussion on the Gradle forums.

Related