I had a utility method for deferring configuration involving tasks that my plugin wasn’t strictly responsible for creating (either directly or via applied plugins) and that technically might not be created at all (similar to ‘tasks.withType’).
It was confusing at first because the ‘tasks’ task would indicate tasks were wired up correctly, but actually running the build would fail since the ‘build’ task was never created.
In hindsight, tasks like ‘build’ would necessarily be present on any real project the plugin would be used with, so it’s safe to use something like ‘findByName’ instead.
Note that findByName() will return null if the build task isn’t there, but if it is there but not created yet (as is the case now with 2.3) it will be created and you’ll have access to it. Therefore, if you check for a null return value and act accordingly everything should still work as before.
Right - the original use case was intended for optional plugins that created a task after being applied and that neglected to use a distinct type for the task (so ‘plugins.withId’ and ‘tasks.withType’ wouldn’t be enough). Admittedly this is an edge case and well-designed plugins should be using distinct types for their tasks.
‘findByName’ will work for the build task as long as I ensure a plugin that provides the build task has been applied first (which I’m already doing). Otherwise it could return null even if a plugin applied after mine would have provided it, and I don’t want to depend on plugin application order.