Determine if a task to be called from a GradleBuild task exists

I have task that is supposed to call a task that is defined in another build.gradle file. However, that task may not exist in the other gradle file. The two gradle files don’t know anything about each other. If ‘taskThatMayNotExist’ doesn’t exist, I want the task to just quietly skip it, but if it does exist, then go ahead and call it. I’ve seen examples with tasks.getByPath and getByName, but those seem to only work if the task belongs to the same project.

task blah(type: GradleBuild) {
            buildFile = directory+ '/build.gradle'
            dir = directory
            tasks = ['taskThatMayNotExist']

So my simple question is how to determine if the build.gradle located at the buildFile path actually contains the task ‘taskThatMayNotExist’?

I’m very much new to Gradle and I’ve been stuck on this for a while. I would really appreciate any help. Thanks.

Did you ever get a solution to this issue? I’m in the same predicament.

My workaround was to create a dummy task in a common grade file which is “applied” by all projects and then projects that want to implement the actual task, create a task with the same name with overwrite=true. Rather clumsy but it worked in my situation.