How to invoke a task from an external project inline instead of creating a new task for it

We have a main task called TaskMain and we need to invoke a task from an external project somewhere in the middle of an existing main task. How to achieve this?
For Ex:
Task TaskMain {


//Invoke External Task here


}

External task is part of some outside project. I found that we can use GradleBuild to achieve this. But the problem is, I need to write a separate task of type GradleBuild to invoke this external task and I can’t invoke this newly written task in the middle of the main task. Is there anyway that I can write the same code inside the existing main task instead of creating a separate task? Following is the new task written:

task buildAndRunSVTTest(type: GradleBuild) {
dependsOn configurations.svttest;
def svtTestDir = "${rootProject.buildDir}/svt-test"
copy {
from tarTree(configurations.svttest.singleFile)
into svtTestDir
}
buildFile = "${rootProject.buildDir}/svt-test/subprojects/apitest/build.gradle"
tasks = [‘runSTQ_e2e’]
}

|