Home > Software engineering >  How to tell spring boot which property files to load according to the server?
How to tell spring boot which property files to load according to the server?

Time:12-02

I have this scenario where I need to load properties file according to the server name.

I have resource in this structure.

I have 2 different servers. let say SERV-A and SERV-B and each of these servers have their sperate e1, e2 servers. eg.. SERV-A-e1, SERV-A-e2, SERV-B-e1, SERV-B-e2. The code base is same.

resource - |
           |-SERV-A - |
                      |-application.properties
                      |-application-e1.properties
                      |-application-e2.properties


           |-SERV-B - |
                      |-application.properties
                      |-application-e1.properties
                      |-application-e2.properties

So I want that when I deploy my application on server SERV-A it should read application.properties and application-e1.properties files from resource/SERV-A and when I deploy it on server SERV-B then it should read from resource/SERV-A

CodePudding user response:

I think you may be able to achieve this using profiles based properties. Docs here. But you may need to play with the folder structure, you may need to bring SERV-B and SERV-A to the application properties name like application-serv-a.properties and application-serv-a-e1.properties. Obviously doesn't scale very much with many servers. There is a similar question here

I'd imagine you can leverage the active profiles like so:

-Dspring.profiles.active=application-serv-a-e1.properties

CodePudding user response:

Thanks @Anton Belev.

I was able to resolve this situation. I recreated my resource structure.

resource - |
           |----------
                      |-application.properties
                      |-application-SerA-e1.properties
                      |-application-SerA-e2.properties
                      |-application-SerB-e1.properties
                      |-application-SerB-e2.propertie

So now when I want to deploy on SERVER-A. I can simply give a simple java command and have it running with correct sets of properties.

java -jar -Dspring.profiles.active=SerA-e1 /path/to/jar/NameOFYourJar.jar &
  • Related