3

I'm trying to pass custom heroku config variable into application.properties but no value is passed.

My application.properties entry:

spring.mail.username=${SPRING_MAIL_SENDER_USERNAME}

Then I inject this value into String variable:

@Value("${spring.mail.username}")
private String SENDER_MAIL_USERNAME;

but the SENDER_MAIL_USERNAME is an empty string when I try to use it.

I can get the proper value like this:

System.getenv("SPRING_MAIL_SENDER_USERNAME");

but this is not what I'm looking for. Anyone knows what I'm doing wrong here?

EDIT:

Maybe it's worth to add that my project uses maven with 2 modules (backend and frontend). Simplified project structure looks like this:

-app  
  -backend  
    -src.main  
      -java.com.jakubeeee.app  
        -some java files...  
      -resources  
        -application.properties  
  -frontend  
    -src   
      -some web files...  

I also use Procfile that contains:

web: java -Dserver.port=$PORT -jar $PATH_TO_JAR

where $PATH_TO_JAR is heroku config variable with value:

backend/target/myjarname.jar
Jakubeeee
  • 666
  • 6
  • 13
  • Hmm, this is what I do in my Spring boot application properties. Are you sure your application.properties file is getting included in your jar properly, for example? What happens when you try to set the environmental variable on your machine? – RyanWilcox Mar 23 '18 at 19:26
  • Application.properties is fine, because i can get values of my hardcoded properties. Setting properties as my machine env variables also works fine and i can get their values with no problem. – Jakubeeee Mar 23 '18 at 21:03

2 Answers2

2

What you show here should work. Double check spelling, etc. Otherwise, you can pass the option in the Procfile as a Java property like this:

web: java -Dspring.mail.username=${SPRING_MAIL_SENDER_USERNAME} -jar target/myapp.jar

Also be sure to confirm that it works with a hardcoded value like spring.mail.username=foo (otherwise you'll know it's something other than the env).

EDIT:

I realize now that you're using a .properties file and not a .yml file. I'm unsure if env vars are supported in Spring's application.properties. You might want to try application.yml config instead.

codefinger
  • 10,088
  • 7
  • 39
  • 51
  • Hardcoded properties work fine. If i use your method with setting the properties in Procfile, then everything starts working fine. Despite this, It would be cool to be possible to set them in application.properties – Jakubeeee Mar 23 '18 at 21:12
  • Environment variables will work for both .properties and .yml files. So that should not be a problem. – Bas de Groot Oct 15 '18 at 11:45
0

There is also very easy way to do it which hasn't been mentioned yet. If your application.properties looks like this:

spring.mail.username=localpassword/dummyvalue

then in heroku you should add new config var with name SPRING_MAIL_USERNAME and assign real/prod password as a value. Then you can inject it to your app like you mentioned so like this:

@Value("${spring.mail.username}")
private String SENDER_MAIL_USERNAME;

and SENDER_MAIL_USERNAME variable will have correct value.

a4dev92
  • 531
  • 6
  • 15