Anandaraja_Srinivasan
Anandaraja_Srinivasan

Reputation: 598

Gradle- Copy Dependencies of Sub Projects into respective folder of Sub Projects

From root, I am basically trying to fetch the dependencies of each sub project and copy into a directory named dependency within each subproject

I have a root Project and in that build.gradle file i have a task like below:

task copyDependencies(type:Copy) {

    nonTestProjects.each {
        delete rootProject.project(it).file('dependencies')
        from rootProject.project(it).configurations.runtime
        intorootProject.project(it).file('dependencies/')
    }
}

Inside the Sub Projects build.gradle, i have dependencies as below:

 dependencies
 {
    implementation "com.google.protobuf:protobuf-java:$protobufVersion"
    implementation "io.netty:netty:$nettyVersion"
    implementation "xmlpull:xmlpull:$xmlPullVersion"
}

On running the task copydependencies from root, i am getting error as below:

Could not get unknown property 'runtime' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfig urationContainer.

Upvotes: 0

Views: 2437

Answers (1)

M.Ricciuti
M.Ricciuti

Reputation: 12096

You get the error Could not get unknown property 'runtime' for configuration container because when gradle configures your root project and tries to create the task copyDependencies, the subprojects have not yet been evaluated, so Gradle doesn't know about "runtime" configuration at this stage ( java plugin has not yet been applied to subprojects).

So one solution would be to wrap this task creation into a gradle.projectsEvaluated lifecycle hook:

gradle.projectsEvaluated {
    task copyDependencies(type:Copy) {
        // task definition ...
    }
}

But then you will have other issues because you want to copy different sources to different destination directories (see How to copy to multiple destinations with Gradle copy task? for possible solution to this issue)

I think a better way would be to create different copyDependencies tasks, one per subproject , and create an "aggregator" task in root project which will depend on these subprojects's tasks:

// aggregator task at root project level
task copyDependencies

// create copydependencies task for each (non-test) subproject
gradle.projectsEvaluated {
    nonTestProjects.each {
        Project proj = project(it)
        Task task = proj.task('copyDependencies', type: Copy) {
            from proj.configurations.runtimeClasspath
            into proj.file("dependencies")
            doFirst {
                file('dependencies').deleteDir()
            }
        }
        copyDependencies.dependsOn task
    }
}

Upvotes: 2

Related Questions