3

I am writing tasks in Gradle to work with my Docker containers. One of them is going to kill and remove 2 containers. Usual command in Linux looks like

docker-compose -f docker-compose.yml kill postgresql redis wap-pattern && docker-compose -f docker-compose.yml rm -f wap-pattern postgresql redis

And it works fine, but in Kotlin I have to use list of arguments, so in my code it looks like

tasks.register<Exec>("downAll") {
    group = "docker"
    commandLine = listOf("docker-compose", "-f", "docker-compose.yml", "kill", "postgresql", "redis", "&&", "docker-compose", "-f", "docker-compose.yml", "rm", "-f", "postgresql", "redis")
}

And unfortunately it doesn't work at all, exiting with error code. Apparently Kotlin doesn't parse && correctly.

So, how can I handle this problem and make my task work? Can I somehow avoid ampersand and call command line execution 2 times in the same task body?

1 Answer 1

4

You can't use &&, but instead you can use the second way to declare task types on gradle.

tasks.register("downAll") {
    group = "docker"
    doLast {
        exec {
            commandLine = listOf("docker-compose", "-f", "docker-compose.yml", "rm", "-f", "postgresql", "redis")
        }
    }
    doLast {
        exec {
            commandLine = listOf("docker-compose", "-f", "docker-compose.yml", "kill", "postgresql", "redis")
       }
    }
}

And if you just want to repeat same commandLine for multiple times I recommend you to use kotlin's repeat function

tasks.register("downAll") {
   doLast {
       repeat(2) {
            exec {
                commandLine = listOf("docker-compose", "-f", "docker-compose.yml", "kill", "postgresql", "redis")
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.