I am developing a spring boot application
I want to override some properties in src/main/resources/application.properties
with an external file (e.g.
Note: The following solution will replace the old application.properties entirely
You can place your application.properties in one of the following locations:
And then exclude the default one under resources directory, like:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/application.properties</exclude>
</excludes>
</resource>
</resources>
</build>
Find more details in this link
If you do not want to use any solution from above, you could do the following in the start of your main(String args[])
method (before the usage of SpringApplication.run(MyClass.class, args)
:
//Specifies a custom location for application.properties file.
System.setProperty("spring.config.location", "target/config/application.properties");
I had the same requirement like yours( war package instead of a fat jar) and I manged to externalize the configuration file: In my main class I made this configuration:
@SpringBootApplication
@PropertySource("file:D:\\Projets\\XXXXX\\Config\\application.properties")
public class WyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(WyApplication.class, args);
}
}
Hope this will help you. Good luck.
1) Tomcat allows you to define context parameters 2) Spring Boot will read the properties defined in Tomcat context parameters as if you are defining them as -Dsomething=some_value
Option 1: Hence a possible way is for you to define spring.config.location at the Tomcat context paramter:
<Context>
<Parameter name="spring.config.location" value="/path/to/application.properties" />
</Context>
Option 2: define a system property at the Tomcat's setenv.sh file
Dspring.config.location=/path/to/application.properties
Option 3: define an environment variable: SPRING_CONFIG_LOCATION
I always use --spring.config.location=
in the command line as specified in the documentation, and you can put various files in this, one with default values and another with the overridden ones.
Edit:
Alternatively, you could also use something like :
@PropertySources({
@PropertySource("classpath:default.properties")
, @PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true)
})
and specify a external.config
in your application.properties.
This would provide a default path for overridding config, that is still overriddable itself by specifying a --external.config
in the command line.
I use this with ${external.config}
being defined as a system env variable, but it should work with a application.properties variable too.