Reputation: 392
I am attempting to externalize some properties and resources; specifically, a properties file and a json file
I have a configuration class:
@Configuration
@PropertySource("sheets.properties")
public class SheetsConfiguration {
@Bean(name="sheets.secrets")
public GoogleClientSecrets clientSecrets(GoogleClientSecretsFactory clientSecretsFactory,
@Value("${sheets.credentials.file}") String credentialFileName) {
return clientSecretsFactory.buildWithResourceCredentials(credentialFileName);
// which calls GoogleClientSecretsFactory.class.getResourceAsStream(credentialsFilePath);
// ${sheets.credentials.file} should resolve to "/sheets.json"
}
//...
}
My launch directory looks like this:
root@foobar:~/# ls ~ -al
total 34292
drwxr-xr-x 4 root root 4096 Mar 8 19:23 .
drwxr-xr-x 3 root root 4096 Mar 7 19:39 ..
-rw-r--r-- 1 root root 35086447 Mar 8 20:50 foo.jar
drwxr-xr-x 2 root root 4096 Mar 8 19:23 logs
-rw-r--r-- 1 root root 436 Mar 8 20:51 sheets.json
-rw-r--r-- 1 root root 91 Mar 8 20:51 sheets.properties
Per the documentation on externalizing configuration, I figured it should be https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/boot-features-external-config.html
root@foobar:~/# java -jar ~/foo.jar --spring.config.location=file:~/sheets.properties
or
root@foobar:~/# java -jar ~/foo.jar --spring.config.additional-location=file:~/sheets.properties
or
root@foobar:~/# java -jar ~/foo.jar --spring-config-name=application,sheets --spring.config.location=file:~/
But every time:
2021-03-08 20:51:13,598 ERROR [org.springframework.boot.SpringApplication] Application run failed org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [foo.Main]; nested exception is java.io.FileNotFoundException: class path resource [sheets.properties] cannot be opened because it does not exist
...
Caused by: java.io.FileNotFoundException: class path resource [sheets.properties] cannot be opened because it does not exist
Everything works fine if the resources are packaged with the jar, but credential information should obviously be externalized.
What am I overlooking in including external properties and resources for spring boot?
Upvotes: 1
Views: 903
Reputation: 1459
Use the absolute path of your properties file in your class like this:
@PropertySource("file:${FOO_PATH}sheets.properties")
Then set its value while starting your app:
java -jar -DFOO_PATH=/your/project/path/ ~/foo.jar
Use \\
as path separators on windows.
Upvotes: 1
Reputation: 18939
Try the following
root@foobar:~/#
java -jar ~/foo.jar --spring.config.name=sheets.properties
By default Spring Boot will override your configuration files based on the location they are placed. By default however it will try to load as property file a file with name application.properties
or application.yml
. Because you have a custom properties file you have to declare it.
As for file location. Outer configuration files override the inner files. So if you have in your resources inside jar a sheets.properties
the outer file in your current directory will override it. You don't need any special property for that.
Hence if you execute your Jar with the above command your properties file from your current directory will be loaded.
Check the following link to understand how to apply externalized configuration and also from which directories spring boot tries to load properties by default.
Spring boot externalized properties
Upvotes: 0