0

I would like to read my profile from an external resource. I have 2 environments one for prod and one for dev. I want my dev env to always load with the dev profile without the need to change the property in the application.properties since it will change in git too if someone pushes this change.

I want each machine to load the profile from external file thus eliminating the need to change application.properties each time someone changes it.

I created a file called profile.properties which is not visible by git and added this line:

spring.profiles.active=test

Then I annotated my class with @PropertySource("classpath:profile.properties") but when I run it I get that the profile is set to default.

I saw the answer here spring-boot-profiles-with-external-properties but still I am not sure how I can read the profile "dev" (as mentioned there) from an external resource.

Gilad Dahan
  • 508
  • 5
  • 19
  • 2
    You can specify the `spring.profiles.active` property in the command-line, or in an environment variable. Both are external to the application, but not in a property file. Why is command-line or environment variable not adequate? – Andreas Jul 01 '21 at 14:42
  • 2
    Why don't you pass the profile via environment variable or VM argument? – Lesiak Jul 01 '21 at 14:43
  • I saw the option in the IDE itself and I worked with it but if I change IDE I won't have it. When you say environment variable you mean in the control panel -> system properties dialog? – Gilad Dahan Jul 01 '21 at 14:45
  • 1
    54.5 Set the active Spring profiles: https://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/howto-properties-and-configuration.html; Just pass in VM args or ENV – phaen Jul 01 '21 at 14:47
  • I added my profile to the environment variables but for some reason it returns null – Gilad Dahan Jul 01 '21 at 14:54
  • 1. I wouldn't set the env variable system-wide on my dev machine - what if you develop another app and you need another profile? 2. Try checking with new terminal window. On windows, new env variables are not loaded into running processes (and child processes inherit them from their parents) – Lesiak Jul 01 '21 at 15:38
  • I was able to load the profile with env variable within the IDE which is intellij. I am thinking what will happen if I switch to eclipse and forget to add this env variable? – Gilad Dahan Jul 01 '21 at 15:55

0 Answers0