0

When defining a property in application.properties such as

deployment=dev
spring.profiles.active=${deployment}

Spring is unable to resolve the deployment property correctly.

Main : The following profiles are active: ${deployment}

Instead, I have to specify a default in each instance of the variable, such as

deployment=dev
spring.profiles.active=${deployment:dev}

However, when I override the property through one of the various ways (environment variable, system property, etc), the set value is correctly resolved everywhere. Is there a way to define a default in the application.properties file?

aarbor
  • 1,398
  • 1
  • 9
  • 24

2 Answers2

0

Here is what you can do:

create an application.properties file that contains the default settings.

Then create an applicaiton-ENV.properties file that contains any addition or overloaded values for the ENV (in your example the ENV would be dev)

Then at runtime set the environment variable spring.profiles.active=dev

java .... -Dspring.profiles.active=dev  YourSpring.jar

This will cause spring boot to run with the profile you want, and will read in the default, and the additional -dev file.

This allows you to set up multiple different application properties for unlimited number of environements. If you set no spring.profiles.active environment variable, it will run simply loading the default application.properties file.

Speckpgh
  • 3,332
  • 1
  • 28
  • 46
  • I know this can be done, but I want a property defined in the main `application.properties` file to be resolvable. For example, I have `db.name=app_${deployment}`. I basically want defaults set so that I don't need to set an environment variable to run the project unless I need to override. – aarbor Jan 29 '20 at 19:45
  • While you certainly can do the db.name=app_${deployment} and work, I don't think you can use the reference model with the spring.profiles.active value. I do not believe is going to properly evaluate your internal reference. After all to get SpringBoot running you need to know the environment you are running, so my suspicion is that is not going to fully evaluate that line in the application.properties file like it would other values which are evaluated further down in the boot sequence I bet. – Speckpgh Jan 29 '20 at 20:26
  • You certainly could do somevar=somevalue db.name=${somevar} but for SpringBoot to boot it needs to know its profile, so I would bet, that line will be evaluated as is and passed to the value, without all the later processing of the app.prop file because that comes later in the boot sequence..>> I could be wrong, but that would be my assumption. After all how can it ensure what each value it should use, if your profile is STG?? It won't read the -stg file from the default file.. it has to SET that value before it can keep going... – Speckpgh Jan 29 '20 at 20:28
  • Imagine the following: application.properties file has "deployment=stg" and then application-stg.properties has "deployment=tst" how could you ever get the value of active Profile if you were allowed to do what you are wanting to do? and have "spring.profiles.active=${deployment}" You can't... – Speckpgh Jan 29 '20 at 20:41
0
${GROUP_ID:123456}

You can indicate what default to be given as part of the property in the above way:

Roe hit
  • 457
  • 1
  • 7
  • 23
  • As I mentioned in the question, I'm already doing that. I just don't want to have to repeat it everywhere that the property exists – aarbor Jan 29 '20 at 19:43
  • I see, Once I've been wanting to achieve something similar, so I created application-local.properties and configured my runtime to use my application-local.properties. Maybe there is a way to define environment as "dev" as you did, and if it is dev , you can try to have springboot pick up all the variables from application-local.properties by default. I'm trying to spin up such a working and hope this suggestion would be helpful, if you haven't found a better solution already. – Roe hit Jan 30 '20 at 13:53