Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 738
heug Avatar asked Jan 14 '20 20:01

heug


People also ask

How do I run a Gradle project from the command line?

To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.

What does Gradle wrapper command do?

Gradle is commonly used by developers to manage their project's build lifecycle. It's the default choice of build tool for all new Android projects. In this tutorial, we'll learn about Gradle Wrapper, an accompanying utility that makes it easier to distribute projects.


4 Answers

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'

like image 169
Eric Avatar answered Oct 23 '22 05:10

Eric


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.

like image 10
matt burns Avatar answered Oct 23 '22 03:10

matt burns


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'
like image 7
andre Avatar answered Oct 23 '22 03:10

andre


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

like image 5
Chayne P. S. Avatar answered Oct 23 '22 04:10

Chayne P. S.