How to pass environment variables to the gradle wrapper build using only command line?
Solution 1
This related post worked for me: https://stackoverflow.com/a/57890208/1441210
The solution was to use the --args
option of gradlew
to get the environment variable to be passed to the spring boot app:
./gradlew bootRun --args='--spring.profiles.active=local'
Solution 2
For passing env variables
MY_ENV_VAR1=xxxxxx MY_ENV_VAR2=xxxxxx ./gradlew bootRun
For arguments/overriding properties values
./gradlew bootRun --args='--spring.profiles.active=local --db.url=something --anotherprop=fafdf'
For both passing env variable and overriding properties values.
MY_ENV_VAR1=xxxxxx MY_ENV_VAR2=xxxxxx ./gradlew bootRun --args='--spring.profiles.active=local --db.url=something --anotherprop=fafdf'
Solution 3
If you want to pass values to the JVM that runs the gradle you can use the '-D' switch. I suppose you have to pass values to the gradle build file from the command line. If that's the case there are two options for that:
-
You can use the -P switch and specify the value there. For example:
gradle -PmySecretKey="This key is so secret" yourTask
-
If you are using linux or variants you can set environment variable as follows:
export ORG_GRADLE_PROJECT_mySecretKey="This key is so secret"
After this you can access the value in the gradle build file as follows (I am using kotlin dsl)
val mySecretKey: String by project
println(mySecretKey)
Solution 4
I just put the env variable setting before calling command as the way a regular Unix shell does. Work with my Zsh.
MY_ENV_VAR1=xxxxxx MY_ENV_VAR2=xxxxxx gradlew clean test
Related videos on Youtube
heug
Updated on July 10, 2021Comments
-
heug almost 2 years
I am trying to pass env variables locally with strictly a command line command. At deploy, these variables get passed into the docker container, but when running locally, they are not present and need to be set locally.
They need to be removed before committing though because they are access keys so i dont want them exposed in the repo. That is why running tests locally (without an IDE) would require a command that passes these variables.
I have already tried this:
./gradlew clean build -Dspring.profiles.active=local -DMY_ENV_VAR1=xxxxxx -DMY_ENV_VAR2=xxxxxx
and it doesnt seem to be working. i cant find the docs for the build command's options, but i thought this was how you pass them. what am i doing wrong here? or is it not possible?
-
heug over 3 yearssetting local env vars in spring is very simple and i know how to do that, i am asking this question because theres a specific case (for PR build checks at my enterprise) for it long story short
-
heug almost 3 yearsand the "--" within the string is necessary?
-
sparkyspider over 2 years@heug indeed it is!
-
Stefan Falk over 2 years
./../gradlew bootRun --args='--spring.profiles.active=angular'
is not working for me.. it's only working if I setSPRING_PROFILES_ACTIVE
but I can't use this because I have to run this in a gradle.build.. somebody fml .. -
Matteo about 2 yearsbut those they do not get passed to my Java application, if I do
MY_ENV=xxxxx ./gradlew buil
. Then in my Java applicationMY_ENV
is null. How can I achieve it? -
Eric almost 2 yearsthis is not the right answer. check my answer for the correct value