I am trying to set environment variables and use them in application.yml of my spring-boot application.
System/OS: windows Terminal: Git Bash
application.yml
datasource:
url: ${DATABASE_URL:jdbc:mysql://localhost:3306/test?useSSL=false}
username: ${DATABASE_USERNAME:root}
password: ${DATABASE_PASSWORD:admin}
Now, to set these environment variables, i did following steps
- create .env file with these environment varaibles
PORT=8000
DATABASE_URL="jdbc:mysql://localhost:3306/test?useSSL=false"
DATABASE_USERNAME=kp-usr-1
DATABASE_PASSWORD=Kpi987223oibnW
- Use
source .env
command to set environment variables for the session
After source .env
I can check and verify all env vars using echo $DATABASE_URL
command,
but it is not picked up by application.yml ( when running ./gradlew build
or ./gradlew bootJar
)
If i set these environment variables in Windows Environment variables from Window settings, then everything works as expected.
Is there anything am i missing ? I want to load the environment variables from .env file for the current session of Java process.
- I tried multiple scripts, like reading lines from .env and calling export=$line, but that also didn`t worked.
- Running
export DATABASE_URL=abd; export DATABASE_USER=admin; TERM=cygwin ./gradlew test
works, but this is cumbersome, when we have too many varaibles. - Note: I am using windows with git bash terminal