How to execute a Gradle task once at the end of subproject tasks

n3utrino picture n3utrino · Apr 23, 2013 · Viewed 18.1k times · Source

I am working with the following project structure

Project
|-Subproject1
|-Subproject2
|build.gradle
|settings.gradle

The submodules are included in the settings.gradle and configured in the build.gradle of the root project.

I have 3 tasks to do

  1. build (every subproject has this)
  2. deploy (this is a packaging mechanism for every subproject needs to work on its own)
  3. finalizeDeployment (this needs to be invoked only once)

I want to be able to call

$gradle deploy <- all subprojects get deployed and finalize gets called once at the end

$gradle Subproject1:deploy <- Subproject1 gets deployed and finalize gets called

build.gradle

configure(subprojects) {

    task build <<{
        println "Do Build "+ project.name
    }

    task deploy(dependsOn:build){
        println 'deploy '+project.name

        doLast{
            finalizeDeployment.execute()
        }
    }
}

task finalizeDeployment{

    dependsOn subprojects.deploy

    doLast{
        println 'Finalize Deployment'
    }
}

Answer

n3utrino picture n3utrino · Apr 24, 2013

It works the following way

configure(subprojects) {

    task build << {
        println "Do Build " + project.name
    }

    task deployPrepare(dependsOn: build)<<{
            println 'deploy ' + project.name
    }

    task deployFinalize(dependsOn: deployPrepare)<<{
        parent.deployFinalize.execute()
    }
}

task deployFinalize {

    doLast {
        println 'Finalize Deployment'
    }
}

deployFinalize.dependsOn(subprojects.deployPrepare)

The Console output for gradle subproject1:deployFinalize is:

Do Build subproject1
deploy subproject1
Finalize Deployment

The Console output for gradle deployFinalize is:

Do Build subproject1
deploy subproject1
Do Build subproject2
deploy subproject2
Finalize Deployment