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

I work with the following project structure

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

Submodules are included in settings.gradle and configured in the build.gradle root project.

I have 3 tasks

  • build (each subproject has this)
  • deploy (this is a packaging mechanism for each subproject that should work on its own)
  • finalizeDeployment (this needs to be called only once)

I want to be able to call

$gradle deploy <- all subprojects will be deployed, and finalization gets called once at the end

$gradle Subproject1:deploy <- Subproject1 is deployed, and finalize receives a call

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' } } 
+4
source share
2 answers

It works as follows

 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) 

Console output for gradle subproject1:deployFinalize :

 Do Build subproject1 deploy subproject1 Finalize Deployment 

Console output for gradle deployFinalize :

 Do Build subproject1 deploy subproject1 Do Build subproject2 deploy subproject2 Finalize Deployment 
+4
source

It looks like you can get the same functionality with dependencies without having to use parent.deployFinalize.execute ().

Where I work, we use:

 subprojects { task dist(type: Copy, dependsOn: assemble) { ... } } task dist dist.dependsOn subprojects.dist task finalizeDist(dependsOn : subprojects.dist) { ... } dist.dependsOn finalizeDist 
0
source

All Articles