Reputation: 183
I have multi-module Maven project with four Spring Boot 2.3.x applications as WAR files. I understand how to manage properties for each application for different profiles (dev, test, qa) when they run independently.
Now I deploy the applications on an external Tomcat 9.x server and I would like to have external property files for every single Spring Boot application.
The property files should be externalized outside Tomcat on the file system like this:
c:/webapp/spring-config/boot-app1-prod.properties
c:/webapp/spring-config/boot-app2-prod.properties
c:/webapp/spring-config/boot-app3-prod.properties
c:/webapp/spring-config/boot-app4-prod.properties
So for my understanding "spring.config.location" is not an option, because I can only specify the location and one property file per Tomcat instance.
I would like to externalize those files only for the active profile 'prod', so this is set:
spring.profiles.active=prod
Question:
What is best practice to achieve this? Spring Cloud Config is not an option at this time.
Upvotes: 0
Views: 758
Reputation: 631
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
return springApplicationBuilder
.sources(ExtpropApplication.class)
.properties(getProperties());
}
public static void main(String[] args) {
new SpringApplicationBuilder(ExtpropApplication.class)
.sources(ExtpropApplication.class)
.properties(getProperties())
.run(args);
}
You can add more then one files using spring.config.name, use the profile conditions before read the file(I didn't add those line on code, based on your preference you can add.)
static Properties getProperties() {
Properties props = new Properties();
props.put("spring.config.name","boot-app1-prod.properties,boot-app2-prod.properties,boot-app3-prod.properties,boot-app4-prod.properties");
props.put("spring.config.location", "file://"+configPath());
return props;
}
If the path will read dynamic and out of war file use the following method to read the files.
public static String configPath() {
File file = new File(".");
String path=file.getAbsolutePath();
int secondLast = path.length()-2;
String destinationPath = path.substring(0, path.lastIndexOf("/",secondLast)+1);
String resourcePath=destinationPath+"/webapps/";
return resourcePath;
}
This is for war deployment and properties location will be dynamic, incase it is static path we can achieve on annotation itself.
Upvotes: 1