Since I have started to migrate to tasks.register, I ran into this issue when trying to depend on an existing task, which may or may not have been defined.
tasks.register throws an exception if the task is already registered, and tasks.named throws an exception if a task is missing.
There are methods to retrieve an optional Task by name (getByName), but not to retrieve an optional TaskProvider, which I must assume means that the task will be configured eagerly.
I have found a protected method in DefaultNamedDomainObject called findDomainObject, that does the trick, but in an unintended way.
Is there any known way to retrieve an optional TaskProvider instead of an optional Task?
Actually, you usually either apply the plugin that adds the task that you want to configure so you know it exists,
or you react to the plugin being applied.
In case of the assemble task, that would be the lifecycle-base plugin.
So you could apply the lifecycle-base plugin (or another plugin that applies it implicitly which should be basically all built-in plugins) and be sure the task exists.
Or you react to it being applied using pluginManager.withPlugin("lifecycle-base") { ... } and know the task is already registered when the closure is executed.