11

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 2

13

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")
}
Sign up to request clarification or add additional context in comments.

1 Comment

How can we do that outside of a task? I would like to use this inside of some buildSrc scripts of mine.
5

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

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

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.