incompletablefuture
incompletablefuture

Reputation: 124

Problem with commandLine in Gradle script on Kotlin

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?

Upvotes: 3

Views: 2657

Answers (1)

Habib Kazemi
Habib Kazemi

Reputation: 2190

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")
            }
        }
    }
}

Upvotes: 4

Related Questions