Created
January 30, 2023 02:16
-
-
Save ericblue/acb761461519ec0c9fae03222a604416 to your computer and use it in GitHub Desktop.
Gradle - Support loading of environment variables from a custom .env file prior to run (works in IDE and CLI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Loads environments variables from files contained in .env located in the CWD' | |
// See: https://stackoverflow.com/questions/36322536/how-to-set-an-environment-variable-from-a-gradle-build | |
run.doFirst { | |
if (project.file('.env').exists()) { | |
file('.env').readLines().each() { | |
if (!it.isEmpty() && !it.startsWith("#")) { | |
def (key, value) = it.tokenize('=') | |
if (System.getenv(key) == null) { | |
println "Setting environment variable for ${key}" | |
environment key, value | |
} | |
} | |
} | |
} else { | |
println "No custom environment variables set" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment