Add a task dependencies from a rule based task to another task

Hello,

I have a multi project with several subprojects. build.gradle defines tasks for the subprojects as well as a rule based task for the root project. The multi project tree looks like following:

root/
build.gradle
settings.gradle
project1/
project2/
project3/

build.gradle:

subprojects {
task (task1) << {
println “Execute task1 on $project.name
}

    task (task2, dependsOn: 'task1') << {
            println "Execute task2  on $project.name"
    }

    task (task3, dependsOn: 'task2') << {
            println "Execute task3  on $project.name"
    }

}

tasks.addRule("Pattern: echo ") { String taskName ->
if (taskName.startsWith("echo ")) {

task(taskName, type: Exec)  {

            def mvnCommand = taskName.split(' ')
            commandLine mvnCommand

    }
}

}

Now I am looking for a solution to add a task dependency from the rule based task to task2 of all subprojects, How can I do that? Later on additional subprojects will be added. This should be possible by just editing the file settings.gradle.

Have you tried dependsOn subprojects.task2?