Java – 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 -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?

Best Solution

This related post worked for me:

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=''