I need some advice wrt modernizing some Gradle cod...
# plugin-development
k
I need some advice wrt modernizing some Gradle code. Specifically, I have some old code that looks like this:
Copy code
tasks.withType(SomeTask).configureEach {
  if (someCondition) {
    enabled = false
    setDependsOn([])
    setFinalizedBy([])
  }
}
I have a few questions about this: • Is the blanking of
dependsOn
and
finalizedBy
necessary? • This doesn't seem like good practice, given that there is no outward guarantee of configure block run order. Is there some kind of alternative if this blanking is deemed necessary?
v
Do you even know whether the condition is already stable, or could it also change until ore even within execution phase?
This detail is import for basically one of two very contrary answers 😄
k
With respect to the condition, it's something derivative of
project.findProperty()
, which actually says nothing, since some uses assume that the value is finalized at configuration time, and other uses assume that this is setting a mutable extra property. For now, I'm going to assume the former.
v
Well, if you want to get a definite result, skipping would be more appropriate using
onlyIf { ... }
as that is calculated at execution time right before the task would get executed. Of course at that time it is too late to manipulate
dependsOn
and
finalizedBy
. Whether those are necessary depends a bit on what you want to achieve. Setting
enabled
to false, or also using
onlyIf { ... }
does only disable that task's actions. Dependency tasks and finalizer tasks will still be run. So if you really want to suppress those along, you probably have to still empty out those and yes, of course later in the configuration cache there could again get some added. If a task is "skipped" due to
-x
/
--exclude-task
, it is not really skipped, but removed from the task graph, including its dependency tasks and finalizer tasks (if not added due to some other reason). If you can evaluate the condition at configuration time, it indeed works to manipulate
gradle.startParameter.excludedTaskNames.add(...)
which then has the same effect as
-x
and thus does not need the other manipulations.