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}
Share:
139,978

Related videos on Youtube

Marcel Overdijk
Author by

Marcel Overdijk

Updated on April 20, 2022

Comments

  • Marcel Overdijk
    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
    jurassix about 6 years
    This was exactly what I needed: app.name=MyApp app.description=${app.name} is a Spring Boot application
  • reverend
    reverend over 5 years
    I 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
    Snackoverflow over 5 years
    Is using default values in configuration UB or is it explicitly stated possible?
  • Edward
    Edward over 5 years
    Just to point out - if you're using kotlin, you need to put your reference in quotes & escape the $ eg root: "\${LOGGING_LEVEL_ROOT:info}"
  • Igor Donin
    Igor Donin over 5 years
    Guys, 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
    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
    Dave Ankin over 3 years
    does this syntax chain? can one do ${MY_EXPECTED_VALUE:${${MY_ENV}_my_expected_prefix:default_v‌​alue}} would be nice to read the docs on this
  • Dave Ankin
    Dave Ankin over 3 years
    can i have semicolons in my default value?
  • wlnirvana
    wlnirvana about 3 years
    is this documented anywhere?
  • ritratt
    ritratt almost 3 years
    I've found that this (and the accepted answer) works for Micronaut's application.yml as well.
  • ch271828n
    ch271828n over 2 years
    Can we make env variable mandatory ? Thanks!
  • Christian Dräger
    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.appl‌​ication.name}}