Make task run always in the end

(Gleb) #1

TL;DR: How can I always execute task in the end of gradle test even if all failed in test.dependsOn …?
my tests use software in docker container, together with some seeding. First I wrote this:
task startContainer {..} task stopContainer {..} seed.dependsOn startContainer test.dependsOn seed test.finalizedBy stopContainer
if seed or startContainer fails, test is never executed, so it is not finalized, and container is never stopped. But I need stopContainer to be executed always when running test task. I need to make sure container is not left running, so it is better to try stopping it even if it was not started, at least I will see it from log.
I tried a hack with
if (gradle.startParameter.taskNames.contains("test")) { // that removes container even if seed failed and test is not started gradle.startParameter.taskNames = gradle.startParameter.taskNames + ["stopContainer"] }
but it only works if I start it with
gradle --continue test because failing seed by default interrupts all. That looks weird.

Q: How can I make sure my container is always stopped?