Can a Gradle project execute a task in another Gradle project (not multi-module)?

Is it possible to define a task in a Gradle project that will invoke a task in an unrelated project? I’m looking for something similar to the Ant task in Ant.

That’s what the GradleBuild task is for.

1 Like

My lack of Groovy knowledge might be letting me down here but I can’t come up with the right syntax to configure and invoke the GradleBuild task. I’ve tried various approaches.

Could you provide an example of how to use this task?

This snippet gives you an idea of what I’m trying to achieve:

task deployConf << {
    gradleBuild {dir '../project1', tasks 'deployConf'}
    gradleBuild {dir '../project2', tasks 'deployConf'}

It’s not so much Groovy but Gradle knowledge that’s needed here. You can’t “invoke” a task from another task. All tasks are top-level, and you can establish task dependencies between them. Try something like:

task gradleBuild(type: GradleBuild) {

dir = “…/project1”

tasks = [“deployConf”]


Ok, thanks for that. I’ve got it working based on your sample.

But on that point about not being able to invoke a task from another task, I’ve got a custom task that invokes the copy task as in the snippet below. Is there something special about the copy task that means it can be invoked in this way?

task stageModules << {
 copy {
  from configurations.runtime.copy().setTransitive(false)
  into stagingDir
  include '*'

You are invoking the ‘copy’ method here, not the ‘Copy’ task. Unless you absolutely need one task to do multiple different things, it’s better to use the task than the method. It’s more descriptive, gives you up-to-date checking, etc. (Only a handful of built-in tasks have an equivalent method in the first place. ‘GradleBuild’ doesn’t.)

Ok, cool. That explains it. Thanks very much.