8

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?

Heinrisch
  • 5,835
  • 4
  • 33
  • 43

2 Answers2

8

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")
}
Rene
  • 5,730
  • 17
  • 20
  • 10
    How can we do that outside of a task? I would like to use this inside of some `buildSrc` scripts of mine. – GarouDan Apr 27 '19 at 13:46
3

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

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