简体   繁体   中英

Gradle Environment variables. Load from file

I am new to Gradle.

Currently I have this task:

task fooTask {
    doLast {
        exec {
            environment 'FOO_KEY', '1234567' // Load from file here!
            commandLine 'fooScript.sh'
        }
    }
}

fooScript.sh

#!/bin/bash
echo $FOO_KEY

Everything works great. But I have env.file with all needed environment variables. This file is used in Docker builder.

env.file

FOO_KEY=1234567

Question: how can I use env.file together with Gradle environment to load all needed env. params?

I give also my version (check if line is not empty and not a comment, also donot override env var):

file('.env').readLines().each() {
        if (!it.isEmpty() && !it.startsWith("#")) {
            def pos = it.indexOf("=")
            def key = it.substring(0, pos)
            def value = it.substring(pos + 1)

            if (System.getenv(key) == null) {
                environment key, value
            }
    }
}

But actually, I think they should add this feature as a exec plugin property! It's quite common now to use .env file.

What about this :

task fooTask {
    doLast {
        exec {
            file('env.file').readLines().each() {
                def (key, value) = it.tokenize('=')
                environment key, value
            }
            commandLine 'fooScript.sh'
        }
    }
}

The following code is the only one i've been able to produce and which satisfies two of the most importants requirements to provide an efficient "UNIX standard environment file import" in Android studio :

  • Loads a file which depends of the Build Type (at least : debug and release)
  • Exposes specified environment variables in the Android code, actually not as environment variables but as buildConfigFields content.
ext {
    node_env = ""
}

android.applicationVariants.all { variant ->
    if (variant.name == "debug") {
        project.ext.set("node_env", "development")
    } else if (variant.name == "release") {
        project.ext.set("node_env", "production")
    }

    file("." + node_env + '.env').readLines().each() {
        if (!it.isEmpty() && !it.startsWith("#")) {
            def pos = it.indexOf("=")
            def key = it.substring(0, pos)
            def value = it.substring(pos + 1)

            if (System.getProperty(key) == null) {
                System.setProperty("env.$key", value)
            }
        }
    }

    if (variant.name == "release") {
        android.signingConfigs.release.storeFile file(System.getProperty("env.ANDROID_APP_SIGNING_STOREFILE"))
        android.signingConfigs.release.keyAlias System.getProperty("env.ANDROID_APP_SIGNING_KEYALIAS")
        android.signingConfigs.release.storePassword System.getProperty("env.ANDROID_APP_SIGNING_STOREPASSWORD")
        android.signingConfigs.release.keyPassword System.getProperty("env.ANDROID_APP_SIGNING_KEYPASSWORD")
    }
    android.defaultConfig.buildConfigField "String", "ANDROID_APP_URL", "\"${System.getProperty("env.ANDROID_APP_URL")}\""
}

Kotlin :

Log.i(TAG, BuildConfig.ANDROID_APP_URL)

Please let me know what you think of it as i'm not completly sure how it works, especially to select the good file to load.

There are plugins to load env vars from a .env file (eg this one )

So a sample build file will look something like this (Kotlin DSL)


plugins {
    id("co.uzzu.dotenv.gradle") version "1.1.0"
}

tasks.withType<Test> {
    useJUnitPlatform()
    //will pass the env vars loaded by the plugin to the environment of the tests
    environment = env.allVariables
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM