Setting environment variables in build.gradle.kts

Viewed 7657

In groovy you can set environment variables with environment key value. For example for run you can do:

run {
    environment DB_HOST "https://nowhere"
}

How can I accomplish this in Kotlin in build.gradle.kts?

2 Answers

Like this:

tasks {
    "run"(JavaExec::class) {
        environment("DB_HOST","https://nowhere")
    }
}

Or if you like the delegation property style:

val run by tasks.getting(JavaExec::class) {
    environment("DB_HOST","https://nowhere")
}

I was having trouble setting environment variables during test runs. This worked for me:

tasks.withType<Test> {
    environment("DB_HOST", "https://nowhere")
}
Related