Call task of another build.gradle file using a groovy method

I have three Gradle projects residing in this kind of folder structure (I have flattened the structure, because i am not sure how to format it with ASCII-Code in this editor):

/intellij-projects/big-project/small-project-AAA/build.gradle
/intellij-projects/big-project/small-project-BBB/build.gradle
/intellij-projects/big-project/small-project-CCC/build.gradle

From within the build.gradle file of the small-project-BBB I want to execute the jar task of the small-project-AAA and small-project-CCC using a groovy method definition in the following fashion:

def triggerTasksInSpecificBuildFile(relativeBuildFilePath, tasksToTrigger) {
    println "Trying to trigger"
    return tasks.create("Trigger", GradleBuild) {
        buildFile = relativeBuildFilePath
        tasks = tasksToTrigger
    }
}

task buildOtherSmallProjects {
    doLast {
        triggerTasksInSpecificBuildFile("../small-project-AAA/build.gradle", ['jar'])
        triggerTasksInSpecificBuildFile("../small-project-CCC/build.gradle", ['jar'])
    }
}

Problem is, when I execute in my bash:

my-user@my-laptop:/intellij-projects/big-project# ./gradlew -b small-project-BBB/build.gradle buildOtherSmallProjects

It only prints “Trying to trigger”, but no jar files are created inside the folders small-project-AAA/build and small-project-CCC/build as they would if I had executed the corresponding jar tasks like this:

my-user@my-laptop:/intellij-projects/big-project# ./gradlew -b small-project-AAA/build.gradle jar
my-user@my-laptop:/intellij-projects/big-project# ./gradlew -b small-project-CCC/build.gradle jar

Just in case: I know that there is the possibility of using a settings.gradle file, but in real life -like actually- the project is much more complex and nested and I cant just introduce such a file, because it would break things elsewhere.

Can someone explain me what I am getting wrong here?

When you execute your buildOtherSmallProjects task, all you’ve coded it to do is print "Trying to trigger" and create new GradleBuild tasks. You never actually try to trigger anything as the Trigger task is never executed in your build.

Ok, how could i trigger the task then? Sorry for the dumb question!

Rather than “triggering” the task, you should change up the configuration a bit. It’s pretty common that people don’t use doLast {} when they should, but you actually have the opposite issue here. All of your code is actually configuration code that should run at configuration time, not at task execution time.

task buildOtherSmallProjects {
    dependsOn createTriggerTask('AAA', '../small-project-AAA/build.gradle', ['jar'])
    dependsOn createTriggerTask('CCC', '../small-project-CCC/build.gradle', ['jar'])
}

def createTriggerTask(projectName, relativeBuildFilePath, tasksToTrigger) {
    tasks.register("trigger${projectName}", GradleBuild) {
        buildFile = relativeBuildFilePath
        tasks = tasksToTrigger
    }
}

I also used register rather than create (task configuration avoidance). This avoids configuring the trigger tasks when you’re not needing to run them, but is not strictly necessary. You can stick to create if you want.

1 Like