'How to pass environment variables to the gradle wrapper build using only command line?

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?



Solution 1:[1]

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 2:[2]

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 3:[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:

  1. You can use the -P switch and specify the value there. For example:

    gradle -PmySecretKey="This key is so secret" yourTask

  2. 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:[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

Solution 5:[5]

Another reason for environment variables not working is the gradle daemon.

Run this to kill any old daemons:

./gradlew --stop

Then try again. Lost far too much time on that.

Solution 6:[6]

To answer your question, as far as I know, there's no way to set environment variables manually through Gradle. What your doing right now is just passing in regular CLI arguments/parameters to your tests.

when running locally, they are not present and need to be set locally.

running tests locally (without an IDE) would require a command that passes these variables.

I see from your snippet, you are using Spring, likely Spring Boot. And since you're already specifying the profile as local, why not define these variables in a profile specific configuration? Example:

application.yml -- base configuration

my-config-value: ${MY_ENV_VAR}

application-local.yml -- local profile configuration that overrides the base

my-config-value: some-dummy-value-for-local-development

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 andre
Solution 3 kaushik
Solution 4 Chayne P. S.
Solution 5 matt burns
Solution 6 Cisco