When I call `./gradlew build` , Gradle executes `b...
# community-support
m
When I call
./gradlew build
, Gradle executes
build
in all projects that contain a
build
task. Is there a way to do the same thing programmatically?
Alright, this is what I have so far:
Copy code
allprojects { otherProject ->
        otherProject.afterEvaluate {
          it.tasks.namedOrNull("build")?.let {
            myTask.dependsOn(it)
          }
        }
      }
Still that
allprojects {}
though šŸ˜•
e
What do you mean by "Is there a way to do the same thing programmatically?"
m
I’d like to have a task in my root project that does the same thing
i.e. it would depend on all ā€œbuildā€ tasks in
allprojects
if present
My end goal is to move as much logic as possible outside of GitHub action yaml into Gradle scripts. i.e change
Copy code
run: ./gradlew build someTask otherTask ...
into
Copy code
run: ./gradlew doWhatsNeeded
p
You could write a convention plugin, apply the base plugin and configure the build lifecycle task to depend on your task
m
Accessing
rootProject.tasks.named("doWhatsNeeded")
from a subproject?
Copy code
rootProject.tasks.named("doWhatsNeeded") {
  dependsOn("build")
}
?
p
Nope:
Copy code
plugins {id("base") }
tasks.build { dependsOn("doWhatsNeeded") }
then you can just call
./gradlew build
m
It’s more the other way around. If I’m on a tag, I wan to push snapshots in addition to
build
for an example
I was aiming to have
doWhatsNeeded
as a root project task but I can see how it’s better to create a separate one in each subproject and leave the ā€œcollectionā€ logic to Gradle
v
Most probably. The only two programmatic ways that do not require modifying all projects or doing cross-project configuration are to either use the tooling API to sub-drive the build again, or to modify the requested tasks in the start parameters at configuration time (yes, that works)
šŸ‘€ 1
šŸ‘Œ 1
šŸ‘ 1