Task 'enabled' vs 'onlyIf'

Hello,

I see in the docs there are actually 2 ways to disable a task execution based on another property: either use the ‘enabled’ property of the task, or use the ‘onlyIf’ method of the task.

  • What are the differences between the two?
  • In which cases the ‘enabled’ property should be used instead of the ‘onlyIf’ property, and vice-versa?

Thanks in advance for your help.
Regards,
Vincent

1 Like

The enabled property is more eager configuration as you’re setting a boolean. If the property changes later, the boolean will not change. It makes sense to use it if the state is always the same. For example, the distribution plugin creates a zip and tar archive. If you only want the tar in any case, you can disable the zip.

The onlyIf is lazier and executed immediately before the execution of the task. This means that if you’re dependent on properties that might update as your build executes, this is an easier way to evaluate them at the appropriate time. I would also prefer onlyIf for more expensive logic as it only will execute when needed as opposed to at configuration time for any task in the build.

2 Likes