This message was deleted.
# community-support
s
This message was deleted.
c
place the logic in a function and add
onlyIf { fn() }
to all relevant tasks.
👍 1
m
Word - I had this partially done but ran into a issue similar to this :
Copy code
register<AggregateThing>("aggregate") {
    dependsOn(named("test", onlyIfFunctionThing()))
}
Where I couldn't configure using
onlyIfFunctionThing
. Is the above pattern not supported?
c
This syntax:
named("test", onlyIfFunctionThing())
is not for `onlyIf`; the second parameter is a configuration action to configure the task.
m
Right, I was using that to configure the task
c
so it returns a configuration action? What’s the relationship to
afterEvaluate
then?
m
Sorry, I used an awful example - I shared that issue to describe the
cannot be executed in current context
issue. I didn't want to do something like this:
Copy code
target.tasks.named("something") {
   ... onlyIf logic
}
Because I wanted that
something
task to be executable without any special conditional logic. Instead, I wanted to do something like:
Copy code
register<AggregateThing>("aggregate") {
    dependsOn(named("something", onlyIfLogic()))
}
So you could execute
something
independently, and have that conditional logic only if you run the
aggregate
task
c
understood. that error is by design - you can’t configure a task while registering another one (that ticket speaks to some of the reasons).
👍 1
m
So is it possible to instruct Gradle to skip those
dependsOn...
tasks if the aggregate one is skipped? Am I modeling this wrong?
c
it looks like you are trying to aggregate some stuff; have you considered using a BuildService to handle that, instead of a separate task? Each task would report into the BuildService, the results could be spit out at the end of the build.
m
Seems to be mostly what I'm doing, maybe I'm not seeing the light at the end of the tunnel: 1. I have a
BuildService
that does some file-path magic to tell me what tasks to run. This is populated by
X
2.
X
runs first - I then want to schedule a bunch of tasks (multiple per module, which is why I have each module define an aggregate task) that should only run if they're in the list that
X
spits out
c
Instead of X -> BuildService, could each task have an onlyIf that calls the BuildService to see if that task should run?
m
That goes back into the concerns I have - ideally, there are two pathways: 1. Someone wants to run some stuff, that stuff should always run without any conditional logic 2. Someone wants to only run some of that required stuff, that stuff runs with the conditional logic
c
yea. I just tackled a smaller version of this, ended up having two sets of tasks - one set for the “aggreate” or “integrated” workflow, and one set for “adhoc”. Perhaps there are better ways (my case was so small I didn’t ponder it deeply).
m
Curious to know what your solution was in more detail! In general, though, did both pathways eventually execute the same task? For example, in the
aggregate
workflow, does it still eventually call `some-project:someTask`(even if this is conditional) and in the
adhoc
workflow, does it also eventually call
some-project:someTask
(non-conditional)?
c
Looking back at my implementation - looks like I never finished it, distracted by pesky forest fire. However, what I did was to create two parallel sets of tasks - different names, same configuration (the config can be pulled out to common functions etc). So you’d have
aggregate
->
:some-project:someAggTask
and
:some-project:someTask
(for ad-hoc use).
🤔 1
m
Had an idea for a bit of additional safety:
Copy code
target.gradle.taskGraph.whenReady {
  if (hasTask(rootTask) 
}
It seems like you can configure whatever you want here! So you can add some
onlyIf {...
conditional logic only if you run
rootTask
c
ah yea. good find.
m
Do you know if this is "safe" to add in terms of build speed, configuration cache, etc? Taking into account all the myriad features is difficult at times
c
`taskGraph.whenReady`is a recommended approach to improve build performance. It doesn’t look to have issues with configuration cache - give it a spin to confirm.
❤️ 1
m
Yup, works as far as I can tell - just wanted to be sure!