I am trying to load an external yml file into my spring boot app
On my classpath, I have 3 yml files for dev prod and tls profiles.
What I intend to do is to load an external file with the name "secret.yml" to override the values on the application-{profiles}.yml file.
This "secret.yml" file contains sensetive information. It will be add to gitignore file.
After some tries, I founded that spring not override the values inside the classpath only if I change the name to application-{profiles}.yml and not secret.yml
I tried to add spring.config.name=secret but that not working for me.
./mvnw -Dmaven.test.skip=true -Dspring.config.additional-location=file:./secret.yml -Dspring.config.name=secret.yml
Have you any solution for that issue ?
[UPDATE]
I do export environment variable export secret="secret.yml"
and then pass the variable to my command line
./mvnw -Dmaven.test.skip=true -Dspring.config.additional-location=file:./secret -Dspring.config.name=secret
Nothing changed
if you pass multiple config file, take care the order, the last one will be override to previous config sequentially.
-Dspring.config.location=classpath:application-1.yaml,classpath:application-2.yaml .. other config
the value of application-2.yaml will be override into application-1.yaml if they have same config.
**That will be merged for different config.
Try to use a absolute path as on spring boot documentation:
java -jar app.jar --spring.config.name=application --spring.config.location=file:///Users/home/secret
If you don't know the absolute path you can find it with pwd command.
All propsitions here works if I wrap my command line to jvmArguments.
./mvnw -Dspring-boot.run.jvmArguments="-Dspring.config.additional-location=file:./secrets.yml"
Thank you for all your reply
Related
I need your help on two issues :
1// I have a spring batch app that has this application.properties file :
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.url=jdbc:oracle:xxxxxxxx
ClassApp=xxxx
Country=xxxxx
spring.batch.initialize-schema=always
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
CRON_EXPRESSION=xxxxx
I want to externalize this configuration to an external file in a specific location and pass it then in the jvm when i run the final jar generated by my application.
Because the jar will be run on a centos machine later and all the variables in the properties file should get their values from that external file !!
How can i do this ?
2// Also, i have some log feature in my app like this one :
log.debug("CreateQuartzJob is running......");
But i want to externalize application logs to an external file also with all execution details too.
How can i make these two features pleaaase ?
Thank you for help :)
As for external configuration, you can use the "additional-location" argument when running your application. Just create a properties or yaml file, e.g., application.yml, and run your jar like this:
java -jar myJar.jar
--spring.config.additional-location=file:/some/directory/application.yml
I have a external configuration file(out side jar). I try to run and expected
that value in external file will override value in internal file(application.properties in \resource\ - in jar file).
I read Documentation and try this:
java -jar ccgame-1.0.jar --spring.config.location=classpath:/application.properties,file:/production.properties
This not working.
My jar file at \target\ directory and my production.properties too(at \target\)
How can I resolve my problem?
Where should I put external config file ?
And what I have to do ?
Starting from Spring Boot 2.0 it's possible to use property spring.config.additional-location. With this property, you can set external config file, but properties from that config will only override the corresponding ones from internal config, leaving other properties unchanged.
More about it in docs.
If you need to completely override the whole config, then continue to use spring.config.location property instead.
By convention, Spring Boot looks for an externalized configuration file – application.properties or application.yml – in 4 predetermined locations in the following order of precedence:
/config subdirectory of the current directory
The current directory
Classpath /config package
The classpath root
You can place your application.properties in any of the 4 locations without needing to give the location of application.properties while executing the jar. If you want to given any other custom location , then you will have to provide the path of the config location while executing the jar:
java -jar -Dspring.config.location=<path-to-file> myProject.jar
Source: https://www.baeldung.com/spring-properties-file-outside-jar
I am using spring boot and I have added another spring boot app as Maven dependency in my project. The problem I am facing is that when I run the application, it picks the property file of the dependency instead of my current application. For example if I run the app using dev profile, application-dev.property file is picked from dependency instead of the running application.
I tried to debug the EnableEncryptablePropertySourcesPostProcessor file and below is the screenshot of the list of property file picked.
Check this out but you have a few options:
Simply specify the config file name:
java -jar myproject.jar --spring.config.name=myproject
And basically you can have myproject-dev.properties
Or directly specify the config files you wanna import:
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Use PropertySource annotation to refer the properties file in your main application file as shown below
#PropertySource(value = { "file:/path/to/folder/file.properties" })
If you have same property in the multiple properties file then one in the classpath will get more preference
**In Application.java file it should be something like this
#PropertySource("classpath:application.properties")
I would like to add a configuration directory to the classpath for a spring boot application at start up, so it can load xml files from the configuration directory.
ie /var/application/config contains
test.xml, dev.xml
The xml will contain mapping information that is required by the application; this is different from application.properties.
I would like to load them at startup.
I am using ClassPathResource to load the files.
Please advise.
You can define your own classpath by the command-line. Lets suppose your jar is myapp.jar and you wand add one extra directory /var/application/config/, so you can execute with the following command line:
java -cp myapp.jar:/var/application/config/ -Dloader.main=myapp.Application org.springframework.boot.loader.PropertiesLauncher
ps: if you are using Windows use ; instead of : to separate your classpath items.
From the Spring Boot Reference Guide, add your config location:
java -jar myproject.jar --spring.config.location=classpath:/var/application/config/
I am trying to load an external properties file into my spring boot app.
initially I used #PropertySource in the config class.
but now I want to remove this annotation so the class is not dependent on the location.
so I tried to use:
java -jar my-boot-ws.war --SPRING_CONFIG_NAME=file:///Users/TMP/resources/
based on this http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html documentation but I get the following error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder
using the annotation works fine but I would really like to move away from that.
any help on this would be great
Thanks
****** CORRECTION *******
Sorry copy paste error the above command was supposed to be:
java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/
I'm not trying to change the name of the config file just add an additional location.
As explained here:
If spring.config.location contains directories (as opposed to files)
they should end in / (and will be appended with the names generated
from spring.config.name before being loaded).
I interpreted this as saying that the file ${spring.application.name}.properties would be loaded from the --spring.config.location passed in from the command line
After some more googeling I found this Spring Boot and multiple external configuration files indicating that the following is the correct usage:
java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/myFile.properties
I was under the impression that the --spring.config.location would load other properties files in the directory specified. according to the post at the link I mentioned this is not the case. based on the link if the directory is specified then that is where the application.properties is searched for. but again the documentation here http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html seems to insinuate that the spring boot app will look on the class path first and if available grab the app name to get additional properties files based on that name.
however once I specified a file name everything worked fine so I guess I was mistaken.
In command line you should use below property to mention an additional boot configuration file:
--spring.config.location="file:/path/to/application.properties"
An alternative would be:
-Dspring.config.location="file:/path/to/application.properties"
Note that characters are lower case and the word separator is a period ..
Otherwise you can use an environment variable with key you used already:
In a *nix system:
export SPRING_CONFIG_NAME=file:/path/to/application.properties
In Windows OS:
set SPRING_CONFIG_NAME=file:/path/to/application.properties
It might not be a common issue, but I faced it. You also must have an application.properties inside your classpath even when you replace it with --spring.config.name (I had mine in gitignore due to sensitive information).
1) Makesure args pass inside of run method
public class GemFireTestLoaderApplication {
public static void main(String[] args) {
SpringApplication.run(GemFireTestLoaderApplication.class, args);
}
}
2) If you have configureed in xml comment or remove first
<!-- <context:property-placeholder location="classpath:config.properties" /> -->
<!-- <context:property-placeholder location="file:/data/xxx/vaquarkhan/dataLoader/config.properties" /> -->
Following command you can use to pass properties name
3.1)
java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties
3.2)
java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
spring.config.name=spring
spring.config.location=classpath:/config/
in side the config folder spring.properties file is available, while running the server the this properties file is not loading