Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
    }
}
like image 765
n3utrino Avatar asked Apr 23 '13 10:04

n3utrino


1 Answers

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
like image 129
n3utrino Avatar answered Sep 22 '22 18:09

n3utrino