I have a number of subprojects that have identical configuration to the above, except for the subproject dependencies. I want to avoid having to duplicate the configureDependencies method and the buildModule task configuration in every subproject.
I’ve tried moving configureDependencies as an extension property into the parent, but this fails to resolve the subproject task paths. Similarly moving the buildModule configuration into the parent fails saying it cannot change the dependencies of the subproject runtime configuration after it has been resolved.
Can you suggest the best way to simplify this and avoid duplication? I’d like to maintain the subprojects as some may require additional configuration.
You are resolving dependencies at configuration time. This will slow down all your builds, even if the dependencies are not needed and also leads to the other problem your described.
What you probably want is:
task extractDependencies(type: Copy) {
inputs.files configurations.runtime
into OUTPUT_DIRECTORY
from configurations.runtime.collect { zipTree(it) }
}
buildModule.dependsOn(extractDependencies)
You’d have to add it as an extension to all sub-projects, so it will be executed in their context. But with the above solution it’s no longer necessary.
One follow up question. If I place this into the parent project build file, and the dependencies in each subproject, I get the following error:
A problem occurred evaluating project ':subproject1'.
> Cannot change dependencies of configuration ':subproject1:runtime' after it has been resolved.
I guess the task is resolving the runtime dependencies of the subproject1 during configuration. Perhaps this can be delayed until the subproject1 dependencies are defined somehow?