'Use single profile-specific property file from multiple application.properties files

I have a few spring-boot microservices and I want to use a single profile-specific property file for all the microservices. profile-specific property file should be outside of the jars.

example:

The application-common-profile.properties file and jar files are inside the same folder

java -jar Microservice-1.jar --spring.config.location=classpath:/application.properties --spring.profiles.active=common-profile

java -jar Microservice-2.jar --spring.config.location=classpath:/application.properties --spring.profiles.active=common-profile

the above commands won't take the common-profile properties. please help to achieve this. Thanks.

IMPORTANT DETAILS

This jar file doesn't contain 'application-common-profile.properties' file. this file resides outside the jar but in the same folder. if I put my 'application.properties' file inside the same foder then it is working(retrieving the properties) with the below command.

java -jar Microservice-1.jar --spring.config.location=C:/folderpath/ --spring.profiles.active=common-profile

I can place other microservices 'application.properties' files in same folder with different names(ex: application-microservice-2.properties). and also the profile property files with different names.(ex: application-microservice-2-common.properties ) It's okay having different application.properties files. but need to have one common profile property file.

FIXED

I added common profile property file(application-common.properties) and jars in same folder and run below commands simply and it is working. I think adding unnecessary arguments was the problem.

java -jar Microservice-1.jar --spring.profiles.active=common
java -jar Microservice-2.jar --spring.profiles.active=common


Solution 1:[1]

If your application.properties file is bundled in your jar, then simply adding the profile-specific property file to your working directory will allow it to be picked up. However, when you set spring.config.location you're overriding the path that Spring will look up these properties from, you should drop this, it's not necessary. Spring will look in your working directory for the properties files by default if they're not bundled in the jar.

Personally, I would avoid trying to maintain the property files in the environments in favor of environment variables.

Bundle your jar with the top-level application.properties included in it, and for the variables that are different given the environment, set environment variables for them.

e.g. if you want to override spring.datasource.driver-class-name, set the environment variable SPRING_DATASOURCE_DRIVER_CLASS_NAME. I think you'll find this approach is far more flexible.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lane.maxwell