For the Zip task, version is a String and it is normally set within the configuration DSL.
I want to set it later when it is needed within the execution phase. The reason is that the version string depends on the action of another task. What I have to do now is invoke this task with a separate gradle run first.
Would like to just run the zip task with the other task as a dependency, but because the zip version is set at configuration, it is as if the first task is not run at all.
Thanks, Sterling. I was hoping to find something that would get this “late binding” behavior by changing the Zip task implementation, and keep everything at the client level still in the configuration phase.
I came across the use of the PropertyState interface (in the guides section), and adapted it in the example below. In the example, the zipRepo version is set at configuration but the actual version string isn’t set until the execution phase. This allows the version of the zip to be the hash of the repo that’s pulled down when the getRepo task is executed. If you commented out the guts of the ZipRepo class, you’ll see it fail at line 69 because the setting of the version has moved to the configuration phase, before the repo has been retrieved.
As a heads-up, PropertyState is deprecated in 4.3 (to be removed in 5.0) for Property, which is the same interface with a better name.
We’ll also be updating the guide you found to suggest the idiomatic ways use Propertys in a custom task (this is supposed to happen in the next ~week).
If your ZipRepo task is very specific to just building zips of git repos, you could also simplify your task to not extend Zip and just use ant.zip() in the task action instead. This is a better choice if you don’t need to expose the full API of CopySpec.
Here are some suggestions to make your tasks a little more idiomatic and lazy:
Thanks for the suggested improvements and the heads-up on the upcoming deprecation. I’ll incorporate your suggestions. Looks like some are not in 4.1, so I’ll have to wait till we upgrade to 4.3 to use.