Search code examples
gradleandroid-gradle-pluginmulti-modulegradle-task

Depend on multiple gradle tasks in multi project build


Currently I have task which starts Google Cloud server, runs tests and stops server. It is defined at root project:

buildscript {...}
allprojects {...}

task startServer (dependsOn: "backend:appengineStart") {}
task testPaid (dependsOn: "app:connectedPaidDebugAndroidTest") {}
task stopServer (dependsOn: "backend:appengineStop") {}
task GCEtesting {
    dependsOn = ["startServer",
                 "testPaid",
                 "stopServer"]
    group = 'custom'
    description 'Starts GCE server, runs tests and stops server.'
    testPaid.mustRunAfter 'startServer'
    stopServer.mustRunAfter 'testPaid'
}

I tried multiple ways to write it something like this, short with only one task. I didn't get how to refer to task from another project and call mustRunAfter on it. This doesn't work (I also tried to refer from Project.tasks.getByPath, root.tasks, etc):

task GCEtesting {
    dependsOn = ["backend:appengineStart",
                 "app:connectedPaidDebugAndroidTest",
                 "backend:appengineStop"]
    group = 'custom'
    description 'Starts GCE server, runs tests and stops server.'
    "app:connectedPaidDebugAndroidTest".mustRunAfter "backend:appengineStart"
    "backend:appengineStop".mustRunAfter "app:connectedPaidDebugAndroidTest"
}

Is it possible? What is correct syntax to make this work?


Solution

  • It looks like your problem is that you're treating dependsOn as meaning "invoke this task". It actually means "ensure the result of the depended on task is available before the dependent task starts". That's why your first solution didn't work: statements like testPaid.mustRunAfter only affect the actions of the testPaid task itself, not its dependencies.

    Anyway, you can get the behaviour you want using dependsOn and finalizedBy, but they have to be declared in the build file of the app subproject.

    app/build.gradle:

    task connectedPaidDebugAndroidTest {
        //
        //...
        //
    
        dependsOn 'backend:appengineStart' // Ensure appengineStart is run at some point before this task
        finalizedBy 'backend:appendginStop' // Ensure appengineStop is run at some point after this task
    }
    

    Then, you can run your tests simply with gradle app:connectedPaidDebugAndroidTest. If you really want to define a task in the root project to run the tests, then that's easy too:

    build.gradle:

    task GCEtesting {
        dependsOn = "app:connectedPaidDebugAndroidTest"
    }