Sbt程序集在JAR内包含Conf文件 [英] Sbt Assembly Include Conf Files inside the JAR
问题描述
我正在使用sbt程序集插件创建一个JAR文件,并且具有以下文件夹结构:
I'm using the sbt assembly plugin to create a JAR file and I have the following folder structure:
src
-main
-scala
-resources
-application.conf
现在,当我这样做
sbt clean assembly
我可以在产生的Jar文件中看到包含application.conf文件.我有两个问题:
I can see in the produced Jar file that the application.conf file is included. I have two questions:
-
为什么包含的application.conf不在最终Jar中的resources文件夹中,而是位于顶层,如下所示(Jar的内容)
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 5月16日21:03 application.conf drwxr-xr-x 3 joe staff 102B 5月16日21:03 com/ -rw-r--r-- 1 joe staff 187B 2016年3月4日library.properties drwxr-xr-x 3 joe staff 102B 5月16日21:03 typesafe/
-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 library.properties drwxr-xr-x 3 joe staff 102B May 16 21:03 typesafe/
如何通过设置System.property加载此application.conf?例如,我希望能够通过设置系统属性来加载application.conf:
How can I load this application.conf by setting a System.property? 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")
System.setProperty("config.file", "application.default.conf")
有了这个,我可以从外部(运行jar文件的同时)控制要使用的配置文件.当我尝试此操作时,我收到了NulPointerException:
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...
推荐答案
第一个问题的答案是资源文件夹就像src文件夹一样,因此内容被复制,而不是文件夹本身.
The answer to your first question is that the resources folder is like a src folder and so the content is copied, not the folder itself.
这篇关于Sbt程序集在JAR内包含Conf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!