Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala jar read external properties file

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?

like image 369
Swapnil Kotwal Avatar asked Jul 26 '16 07:07

Swapnil Kotwal


People also ask

How do you pass external properties in spring boot jar?

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.

How do I change the properties of a jar 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.

How do you call external properties from spring boot?

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


1 Answers

The class loader will load the file from the location it finds first.

In your case, the file exists in two places:

  • Inside the current project itself
  • Inside a jar dependency

Which 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.

like image 136
janos Avatar answered Oct 20 '22 20:10

janos