Add configuration directory to classpath using spring boot - java

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/

Related

externalize spring configuration and logs

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

Externalized Configuration in Spring boot

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

spring boot external config with sensitive information not working

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

Boot picking up property file from dependency instead of application

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")

spring boot: add new yml files to application config

i want developers to be able to locally override some configuration properties. (let's say we work on google drive and everyone should test it on its own account).
i don't want to override properties using command line (because it has to be set inside every IDE configuration and on every CLI run).
what i want is: application should use all the standard spring boot config files (application.yml etc) and also look for e.g. local.yml (on the classpath) or some file inside user.home. and those additional files should override other settings.
how to add new yml resources and order them correctly?
edit: i know spring's default orders and locations. question is about adding new ones
If you look in the Spring Boot documentation about the locations for configuration files (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config), you can see, that they are loaded from the following places (amongst others):
Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
Application properties outside of your packaged jar (application.properties and YAML variants).
There are two default locations where they are loaded from ( see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files):
A /config subdirectory of the current directory.
The current directory
Current directory in this case means the working directory for the Java process (Usually the directory where the JAR is located, or in case of running with in the IDE, usually the project root folder). So the developers just can place their own configuration files in that places and they are automatically loaded (and will override properties within the JARs). Add that files to .gitignore (or .svnignore or ...) and they won't accidentally committed into your repository.
There's a new way to do this, after Spring Boot v2.4, by using spring.config.import: https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4#importing-additional-configuration
By adding this part to your application.yml file, you should be able to import the additional configuration:
spring:
config:
import: local.yml
The article also has this section:
Imports can be considered as additional documents inserted just below the document that declares them. They follow the same top-down ordering as regular multi-document files: An import will only be imported once, no matter how many times it is declared.
So the contents of local.yml should be handled as if they were appended to the end of application.yml, thereby allowing you to override any property in application.yml.
From Spring Boot Documentation : Application property files:
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
This also goes for yaml, so you everyone can add application.yml under config directory, under the directory you run the spring boot jar from.
You can also customize the extra configuration file to be local.yml if you'd like by using spring.config.location:
--spring.config.location=classpath:/application.yml,classpath:/local.yml
Note however:
spring.config.name and spring.config.location are used very early to determine which files have to be loaded so they have to be defined as an environment property (typically OS env, system property or command line argument).
To provide the configuration from external config file in spring-boot application -
-Dspring.config.location=file:/home/vfroot/Workspace/project/MODULE_HOME/application.yaml
this command can be run with terminal:
mvn clean install -Dspring.config.location
= file:/home/vfroot/Workspace/MODULE_HOME/application.yaml
or need to set in Eclipse VM argument.
Also to set the active profiles :
-Dspring.profiles.active=dev
Well, since i am new in Spring Boot & Restfull Web Services. However, i managed to add a new .yml file to mange database and server port.
Instructions that i followed:
Project File.
Other Sources
src/main/resources
default package
right click on "default package"
add new YAML FILE
Or of YAML File option not available
5. right click on "default package"
6. then in categories: other --> File Types: YAML File

Categories

Resources