Assume I’d like to create a new task in a plugin. The name of task is built dynamically based on the value of an extension provided by the plugin.
The following code describes what I do in the plugin:
class MyExtension {
String name
}
project.extensions.create('custom', MyExtension)
project.task("${project.custom.name}Task")
In the build script that applies the plugin, I set the value of property exposed by the extension:
apply plugin: 'myPlugin'
custom {
name = 'test1'
}
Without wrapping the task creation code into an afterEvaluate block, the extension property value will be null. This makes sense as the extension is not evaluated yet. Is there a different/better way to achieve this without using afterEvaluate. Alternatively, is there a way to delay setting the task name?
We are trialling an idea for deferring configuration more generally in 1.5 on with the publishing extension. Look at @DeferredConfigurable in the codebase and its uses. This is an incubating feature though.
I can’t offer any other advice without knowing more about the use case.
I just had a look at the DeferredConfigurableExtensionIntegrationTest. Looks like a promising approach. I will need to play around with it over the weekend.