Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active April 11, 2022 01:49
Show Gist options
  • Save sandipchitale/6671faf45d3a9178e8d9b314433665d2 to your computer and use it in GitHub Desktop.
Save sandipchitale/6671faf45d3a9178e8d9b314433665d2 to your computer and use it in GitHub Desktop.
MayFail #gradle
def random = new Random();
ext {
__anyFailed = false;
__failedTasks = [];
__failures = [];
__allowFail = [];
}
// gradle.taskGraph.whenReady { TaskExecutionGraph teg ->
// teg.allTasks.each { Task task ->
// if (task.ext.has('mayFail') && task.ext.mayFail) {
// println 'May Fail:' + task.path
// } else {
// println 'Must Pass:' + task.path
// }
// }
// }
gradle.taskGraph.afterTask { Task task, TaskState state ->
if (task.ext.has('mayFail') && task.ext.mayFail) {
if (task.state.failure != null) {
project.ext.__anyFailed = true;
project.ext.__failedTasks.push(task);
project.ext.__failures.push(task.state.failure);
task.state.failure = null;
}
}
}
gradle.buildFinished { BuildResult br ->
println '-------------------------------- Build Finished'
if (project.ext.__anyFailed) {
project.ext.__failures.each { failure ->
println failure
}
}
}
tasks.register('hello') {
ext.mayFail = true
doLast {
if (true) {
throw new TaskExecutionException(it, new RuntimeException("Failing ${it.name}"))
}
}
}
tasks.register('goodbye') {
ext.mayFail = false
doLast {
if (random.nextBoolean()) {
throw new TaskExecutionException(it, new RuntimeException("Failing ${it.name}"))
}
}
}
tasks.register('lasttask') {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment