sparkr sparkr -3 years ago 208
Scala Question

Sbt Assembly Include Conf Files inside the JAR

I'm using the sbt assembly plugin to create a JAR file and I have the following folder structure:


Now, when I do

sbt clean assembly

I can see in the produced Jar file that the application.conf file is included. I have two questions:

  1. Why is the inluded application.conf not inside the resources folder in the final Jar, but rather it is on the top level as shown below (the contents of the Jar)

    -rw-r--r-- 1 joe staff 272B May 16 21:03 application.conf
    drwxr-xr-x 3 joe staff 102B May 16 21:03 com/
    -rw-r--r-- 1 joe staff 187B Mar 4 2016
    drwxr-xr-x 3 joe staff 102B May 16 21:03 typesafe/

  2. How can I load this application.conf by setting a For example., I want to be able to load the application.conf by setting a System property like:

    System.setProperty("config.file", "application.default.conf")

With this I could control from outside (while running the jar file), which config file to use. When I tried this, I got a NulPointerException:

val someConfigFile = Option(System.getProperty("config.file"))
ConfigFactory.parseURL(getClass.getResource(someConfigFile.get)) // NullPointerException happens here...

Answer Source

Here is what I needed to do so that the config file inside the jar could be loaded based on what I specify from outside!

  val is = new InputStreamReader(getClass.getResourceAsStream(s"/$cfgFile"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download