Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting environment variables in build.gradle.kts

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?

like image 607
Heinrisch Avatar asked Nov 09 '18 09:11

Heinrisch


People also ask

Is it possible to set environment variables from a Gradle build?

Is there any way to set an environment variable from a Gradle build (in the build.gradle file)? I've tested it in isolation: the values do get passed and my test task receives everything, be it a systemProperty, environment variables or jvmArgs. So, it's nothing wrong with Gradle itself here.

Where are Gradle properties stored in Linux?

Gradle properties such as org.gradle.caching=true that are typically stored in a gradle.properties file in a project root directory or GRADLE_USER_HOME environment variable. Environment variables such as GRADLE_OPTS sourced by the environment that executes Gradle.

How do I PASS system properties to Gradle?

System properties Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle.properties files with the prefix systemProp.

How to configure Gradle behavior?

Gradle provides multiple mechanisms for configuring behavior of Gradle itself and specific projects. The following is a reference for using these mechanisms. When configuring Gradle behavior you can use these methods, listed in order of highest to lowest precedence (first one wins): Command-line flags such as --build-cache.


Video Answer


1 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")
}
like image 159
Rene Avatar answered Nov 15 '22 10:11

Rene