How to override internal configuration file in Spring Correctly? - java

I have an internal application.yml configuration file located in the resources folder on classpath.
I have an external configuration file: /home/username/config.properties which overrides some fields to run in a server context.
I want the fields in the external config file to override internal file and retain the fields in the internal file if not defined in external file.
The answers suggested Spring - how to override internal config file with external file doesn't work.
For example, the following command doesn't works for me:
java -jar application.jar --spring.config.location=classpath:/,file:///home/minister/config.properties
How do I achieve this desired outcome?
EDIT: This issue only happens on Linux. When I run it with the overriden configuration file on my Windows 10 machine, it works properly.

Looks like you have one too many /, try:
java -jar application.jar --spring.config.location=classpath:/,file:/home/minister/config.properties

You can pass the path to file, relative to the .jar file, for example,
java -jar application.jar -Dspring.config.location=./config/application.properties
or
java -jar application.jar -Dspring.config.location=../common/config
either of these work. Please refer - Spring Documentation for further details on how externalized configuration works.
Hope this helps.

Related

spring.config.location - properties overloading with multiple files

I'm trying to configure my Spring application.
I have in /src/main/resources my file application.yml. In my main class there is no #PropertySource annotation (but I tried also using that).
I also have another file on my Windows (/c/Programs/application.yml) that has similar content (some values are overriden).
I have to run application with following cmd call (my organization doesn't want me to use another call, because it part of CI/CD standard process):
java -Dspring.config.location=classpath:/application.yml,/c/Programs/application.yml -jar app.jar
My /src/main/resurces/application.yml file is included with .jar after building app. Running app with cmd above uses keys from resources, not from second file.
As I gathered from few hours of trying to solve this:
with two files spring.config.location, second one overrides values from first one,
files should be separated by comma,
I don't have to use spring.config.name, when I specify single file (not directory).
What is the problem with my configuration? How can I override file from resources with external file?

Setting log4j properties file via JVM argument - why does order matter?

I have built a jar file which has a log4j.properties file in it (mvn package put it there by default from the resources directory). But when I run this jar file, I want to pass a different logging config, so I add -Dlog4j.configuration=file:{path to file}. The issue that bugs me is that the order matters here as follows:
When I run java -jar {path to jar} -Dlog4j.configuration=file:{path to file} then it reads the log file packaged in the jar.
When I run java -Dlog4j.configuration=file:{path to file} -jar {path to jar}, then it reads the config from the file I pass in the parameters.
I have rough understanding how classpaths work in java and that if I were to load several java classes with the same name, it would make a difference. But this way I am passing a config parameter with a -D prefix, so the way I expect this to work is for some code in log4j library to check whether -Dlog4j.configuration is set and if so, then load the config from there, otherwise try to find it on the classpath.
Any ideas on what I am missing?
If you provide anything after naming the JAR file, it is treated as an argument to your main method. For Log4J you actually have to define a property, and this needs to be done before you specify -jar.

How to execute Hadoop Jar with external properties

I have a jar application that works with an external properties file. The file is used so that the user can override default properties. The default and core properties are part of the build so no problem there.
Normaly I would do something like one of theese:
java -jar MyAwesomeApp.jar user.properties
OR
java -jar MyAwesomeApp.jar -Dmyapp.userproperties=user.properties
But with hadoop, the jar gets executed inside the hadoop framework like this
/bin/hadoop jar MyAwesomeApp.jar input output
And no matter where I put the -D I can't get the value via System.getProperty(...). The properties is not set. The hadoop documentation said that -D is a GENERIC OPTION and is set after the command. But if I do so I get an error that -D is not a valid jar file (duh...)
I aim to keep the application as clean as possible...so I only want to pass the user configuration as a parameter as a last resort, i.e.
/bin/hadoop jar MyAwesomeApp.jar input output user.properties
I hope someone can tell me what I have to do to get the -D working :/
Hadoop is running pseudo distributed so I am actually using HDFS too...

Externalizing properties files in spring boot

I am trying to externalize the properties file of my project.
Steps to run:
Created a jar file without properties file.
Run these script file from command prompt.
.
java -jar read-apis.jar --spring.config.location=classpath:..\config\application.properties,classpath:..\config\sql-config.properties,classpath:..\config\error-config.properties,classpath:..\config\messgae-config.properties,classpath:..\config\validation-config.properties
OR
java -cp ..\config\application.properties, -cp ..\config\sql-config.properties, -cp ..\config\error-config.properties, -cp ..\config\messgae-config.properties, -cp ..\config\validation-config.properties -jar read-apis.jar
Its not working for me please help me.
I am basing information from the Spring Boot documentation here and my own experience. From what I can tell you have a configuration directory at ../config. You can either:
Put the config directory at the location where you run the application from. If the config directory is located at . instead of .. it will be picked up without any additional parameters.
OR leave it there and use something similar to your first form like this: "spring.config.location=file:..\config\application.properties". Since it is not in the jar you will need to use "file" instead of "classpath".
Give that a try and see if it works. It looks like you are trying to put multiple files in the search list. It may work but I am not certain. If that is the case then the first bullet above may not work since only application.properties would be searched for in the config directory. You could always add the other files in using the config property since it looks like it always uses the default paths also.
java -Dspring.config.location=application.properties,sql-config.properties,error-config.properties -jar read-api.jar
This works out for me.

Problem with incrementally setting up classpath when running a jar file

I have an application contained in A.JAR. This jar has several dependencies so they are specified in the manifest as "lib/B.JAR lib/C.JAR lib/D.JAR". I have my installation directory with A.JAR, and under it I have the lib directory with the three others.
I go to my installation directory and run "java -jar A.JAR" to run the application, and it starts running. However, it looks for log4j.properties for setting up log4j. Now as far as I know, log4j.properties needs to be in the classpath.
Now let's suppose I want to run several instances of A.JAR, but with various log4j properties. So I setup 4 installation directories (inst1, inst2,...) and I've put A.JAR, a customized log4j.properties and the lib directory in each of them.
Is this the right way to go (forget about the copying itself, I can do this with symlinks)? Maybe there's a way for telling log4j to look for the properties in a specific place using some define (-D) in runtime?
If what I've described is a good setup, how can I actually run it? Java doesn't "catch" the log4j.properties as part of its classpath. running "java -classpath . -jar A.JAR" wasn't helpful as well.
You can use the log4j.configuration system property to define the properties files you want log4j to use.
A simple example for a file in some directory would be:
java -Dlog4j.configuration=file:/c:/foobar.properties YOUR CLASS PATH -jar JAR FILE
For more information check the documentation. Especially check the Default Initialization Procedure section.

Categories

Resources