Can't add dependent tasks to dependencies task

We convert cookbook dependencies (Berksfile) to gradle and add them as dependencies.

task loadDependencies << {

… //Convert Berksfile dependencies …

for ( d in metadata.dependencies ) {

project.dependencies { cookbook group: project.group, name: d.key, version: chefConstraintToIvyConstraint(d.value), configuration: “cookbook” }

}

}

When I execute dependencies task, its not resolving anything as there are no dependencies. So I tried adding dependencies task to dependsOn this task, but it’s throwing below error.

No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated.dependsOn() is applicable for argument types: (org.gradle.api.DefaultTask_Decorated) values: [task ‘:test123’]

You’re running into a common misunderstanding. Your loadDependencies task is performing configuration during the execution phase. That means Gradle has already finalized the list of dependencies and the dependencies you add are ignored.

You can try putting your “Convert Berksfile dependencies” code outside of a task to start.

Your second error is from a clash between the names of the ‘dependencies {}’ top level element and the ‘dependencies’ task. I think it would have worked (but still would have done the wrong thing) if you had done tasks[“dependencies”].dependsOn loadDependencies.