简体   繁体   English

Typesafe配置加载错误的配置

[英]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.confdev.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.

相关问题 具有typesafe配置的Scala / Slick JDBC编码配置 - Scala/Slick JDBC encoding configuration with typesafe config 类型安全配置:如何迭代配置项 - Typesafe config: How to iterate over configuration items 未找到关键类型安全配置的配置设置 - No configuration setting found for key typesafe config 在类型安全配置中使用 withFallBack 合并配置中的数组值 - Merge array values in configuration using withFallBack in typesafe config com.typesafe.config.ConfigException $ Missing:找不到密钥的配置设置 - com.typesafe.config.ConfigException$Missing: No configuration setting found for key 使用typesafe / akka config合并多个配置级别 - merging multiple levels of configuration using typesafe/akka config 类型安全配置:获取为 map - Typesafe config: get as map 无法运行Scala Play应用并抛出com.typesafe.config.ConfigException $ Missing:找不到密钥的配置设置 - Unable to run Scala Play app throwing com.typesafe.config.ConfigException$Missing: No configuration setting found for key 如何从typesafe配置配置系统属性或logback配置变量? - How can I configure system properties or logback configuration variables from typesafe config? com.typesafe.config.ConfigException $ Missing:找不到关键'play'的配置设置 - com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'play'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM