Execute task onlyIf other task will execute

I’m curious if there is a way to execute a task only if another task will execute. For me this would be useful to trigger starting up tomcat before my integration tests, but only if the integration tests need to run. For example, when using the Tomcat Gradle Plugin I would like integrationTomcatRun to only run if integrationTest needs to be ran.

task integrationTomcatRun(type: org.gradle.api.plugins.tomcat.tasks.TomcatRun) {
  // ??
onlyIf { integrationTest.?? }
  task integrationTest(type: Test) {
    dependsOn integrationTomcatRun
    finalizedBy integrationTomcatStop

If you can’t accomplish this using the various task dependency directives, you can access project.gradle.taskGraph to find out if a task is present in the graph.

Thank you for your reply. My question (perhaps not very clear) is not regarding if the task is on the task graph. My question pertains to the task’s up to date check. For example, if I run “gradlew build” after making a change I want everything to run. If I run “gradlew build” again, then everything should be up to date and thus “integrationTomcatRun” should not run since the tests do not actually need to be ran again.

Ah, makes sense. I’m not aware of a way to do this, since I believe the up-to-date checks don’t get invoked until task execution.