Sbt 程序集在 JAR 中包含 Conf 文件

Sbt Assembly Include Conf Files inside the JAR

我正在使用 sbt 程序集插件创建一个 JAR 文件,我有以下文件夹结构:

src
 -main
  -scala
  -resources
    -application.conf

现在,当我这样做时

sbt clean assembly

我可以在生成的 Jar 文件中看到包含 application.conf 文件。我有两个问题:

  1. 为什么包含的 application.conf 不在最终 Jar 的资源文件夹中,而是位于顶层,如下所示(Jar 的内容)

    drwxr-xr-x    3 joe  staff   102B May 16 21:03 com/
    -rw-r--r--    1 joe  staff   187B Mar  4  2016 library.properties
    drwxr-xr-x    3 joe  staff   102B May 16 21:03 typesafe/
    
  2. 如何通过设置 System.property 加载此 application.conf?例如,我希望能够通过设置系统 属性 来加载 application.conf,例如:

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

有了这个我可以从外部控制(而 运行 jar 文件),使用哪个配置文件。当我尝试这个时,我得到了一个 NulPointerException:

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

这是我需要做的,以便可以根据我从外部指定的内容加载 jar 中的配置文件!

  val is = new InputStreamReader(getClass.getResourceAsStream(s"/$cfgFile"))
  ConfigFactory.parseReader(is).resolve()

第一个问题的答案是资源文件夹类似于 src 文件夹,因此复制的是内容,而不是文件夹本身。