I have written some code and exported it as a jar file. In this jar there is a file named automation.properties
with defaults that I'm loading using
val automationPropertiesFileURL = getClass.getResource("/automation.properties")
if (automationPropertiesFileURL != null) {
val source = Source.fromURL(automationPropertiesFileURL)
config = new Properties()
config.load(source.bufferedReader())
}
But when this jar file gets added as a gradle
dependency in C:\User\abc\.gradle
and I want to read automation.properties
from my current project, how can I override the location and read the file from my project and not from the jar file itself?
Actually, the most easiest way is to put application. properties and your. jar into the same directory, and just java -jar your. jar will automatically load this external config file.
Use 7zip, right click on jar and with 7z say open archive, then go to property file, double click and open file with notepad, edit it and save. Now when you will close 7z console, it will ask "update archive?" Say yes... That's it. Save this answer.
properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring. config. location environment property (comma-separated list of directory locations, or file paths).
The class loader will load the file from the location it finds first.
In your case, the file exists in two places:
jar
dependencyWhich file will be found by the class loader,
depends on the ordering of the "current project" and the jar
dependency on the classpath.
That's what you need to review,
that's the key to loading the right file.
Your current code is correct as it is, this is a matter of classpath configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With