Does application.yml support environment variables?
139,978
Solution 1
Try ${OPENSHIFT_DIY_PORT}
(the usual Spring placeholder notation). See here for docs.
Solution 2
You even can add default value, if environment variable not provided:
logging:
level:
root: ${LOGGING_LEVEL_ROOT:info}
Related videos on Youtube
Author by
Marcel Overdijk
Updated on April 20, 2022Comments
-
Marcel Overdijk about 2 years
I tried using env variables in my application.yml configration like:
spring: main: show_banner: false --- spring: profiles: production server: address: $OPENSHIFT_DIY_IP port: $OPENSHIFT_DIY_PORT
but the env variables are not resolved. Do I have to provide a different notation?
In Rails you can e.g. use <%= ENV['FOOVAR'] %>
The only alternative is to run the app like:
java -jar my.jar --server.address=$OPENSHIFT_DIY_IP --server.port=$OPENSHIFT_DIY_PORT
-
jurassix about 6 yearsThis was exactly what I needed:
app.name=MyApp app.description=${app.name} is a Spring Boot application
-
reverend over 5 yearsI found this syntax in an app I inherited and have been unable to find the Spring docs that show this. It is fairly obvious what is happening, but to be able to find the docs for it would be nice. Thank you for sharing - I am more confident with my conclusion now.
-
Snackoverflow over 5 yearsIs using default values in configuration UB or is it explicitly stated possible?
-
Edward over 5 yearsJust to point out - if you're using kotlin, you need to put your reference in quotes & escape the
$
egroot: "\${LOGGING_LEVEL_ROOT:info}"
-
Igor Donin over 5 yearsGuys, how can we go about passing the OPENSHIFT_DIY_PORT through unix cli when starting the application? I know we can use -D to pass override parameters, but does that also work for env variables? Ex.: nohup java -Xmx1024m -jar -Dspring.profiles.active="whatever". Is there a way to do that with env vars?
-
PAX about 4 years@IgorDonin, would concatenation of variable assignments and program call an option for you? E. g.:
$MY_ENV=value && java -jar ...
-
Dave Ankin over 3 yearsdoes this syntax chain? can one do
${MY_EXPECTED_VALUE:${${MY_ENV}_my_expected_prefix:default_value}}
would be nice to read the docs on this -
Dave Ankin over 3 yearscan i have semicolons in my default value?
-
wlnirvana about 3 yearsis this documented anywhere?
-
ritratt almost 3 yearsI've found that this (and the accepted answer) works for Micronaut's application.yml as well.
-
ch271828n over 2 yearsCan we make env variable mandatory ? Thanks!
-
Christian Dräger about 2 years@DaveAnkin syntax can be chained. i just tried out:
spring.datasource.url: ${POSTGRES_URL:jdbc:postgresql://database:5432/${spring.application.name}}