[英]Typesafe config loads wrong configuration
So the problem is really simple and I hope solution will be as well. 因此,问题确实很简单,我希望解决方案也将如此。
So basically I have two configuration files application.conf
and dev.conf
. 所以基本上我有两个配置文件
application.conf
和dev.conf
。 I'm passing my config files from command line like that sbt -Dconfig.file=dev.conf
. 我正在从命令行传递我的配置文件,如
sbt -Dconfig.file=dev.conf
。
The problem is when I use ConfigFactory.load
from main object(the one which extends App
) it loads config I passed via command line(in this case dev.conf
), but when I load the config from different object it loads default application.conf
. 问题是,当我从主要对象(
extends App
对象)使用ConfigFactory.load
,它会加载通过命令行(在本例中为dev.conf
)传递的配置,但是当我从其他对象加载配置时,它将加载默认application.conf
。
Can I load somehow config passed from arguments from any object? 我可以加载从任何对象的参数传递来的配置吗?
When you run your application with the runMain
SBT task, then by default SBT won't create a separate JVM for your code. 当您使用
runMain
SBT任务运行应用程序时, 默认情况下, SBT不会为您的代码创建单独的JVM。 This has several consequences around the application lifecycle, and of course with regard to system properties as well. 在应用程序生命周期中,当然还有系统属性方面,都会产生多种后果。
In general, your approach should work, as long as your build configuration does not enable forking . 通常,只要您的构建配置未启用fork ,您的方法就应该有效。 However, I think the better approach would be to actually rely on forking and specify the system property explicitly.
但是,我认为更好的方法是实际上依赖于派生并显式指定系统属性。 This is guaranteed to work.
这样可以保证工作。 To do this, you need to set the
fork
setting in the run
task to true
, and then add a JVM command line option: 为此,您需要将
run
任务中的fork
设置设置为true
,然后添加一个JVM命令行选项:
Compile / run / fork := true,
Compile / run / javaOptions += "-Dconfig.file=dev.conf",
Don't forget to restart SBT after that. 之后不要忘记重新启动SBT。 You won't need to pass the
config.file
property to SBT with this approach; 您无需通过这种方法将
config.file
属性传递给SBT; rather, it is controlled by the javaOptions
setting, as in the example above. 而是由
javaOptions
设置控制,如上例所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.