I have a multi-project Gradle project, and in the root project, I have the following:
task timeStamp {
doLast {
Date now = new Date()
println "All completed @ $now"
}
}
allprojects {
afterEvaluate {
it.getTasks().each {
println "finalize set for \"$it.name\""
if (it.name != "timeStamp") {
it.finalizedBy(':timeStamp')
}
}
}
}
In my output when running gradlew build
, the timeStamp task is run, but it is run before some of the other tasks.
How can I get this time stamp to be printed after all other output? (I prefer it to directly precede BUILD SUCCESSFUL in ##s
in the output, if that is possible)
If you simply want to print timestamp at the end of the build (after last task is executed) you can implement as follows:
gradle.getTaskGraph().whenReady { graph ->
graph.getAllTasks().last().doLast {
Date now = new Date()
println "\n ***** All completed @ $now ***"
}
}
This way, you will add an action to the last task to be executed as determined by the task execution graph computed by Gradle.
You can also use buildFinished hook, but the callback will be executed after the BUILD SUCCESSFUL in ##s
message
gradle.buildFinished {
Date now = new Date()
println "\n ***** All completed @ $now ***"
}
result with command ./gradlew classes
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 up-to-date
***** All completed @ Thu Sep 20 21:15:03 CEST 2018 ***
With both solutions, this callback will be executed whatever task you are executing (gradle build, gradle clean, *gradle assemble** , ..)
you need to finalize assembleDebug
and assembleRelease
(or however they may be called):
task finalizeBuild {
doLast {
println(":finalizeBuild > doLast")
}
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleDebug' || task.name == 'assembleRelease') {
task.finalizedBy finalizeBuild
}
}