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
. I'm passing my config files from command line like that 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
.
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. 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. 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:
Compile / run / fork := true,
Compile / run / javaOptions += "-Dconfig.file=dev.conf",
Don't forget to restart SBT after that. You won't need to pass the config.file
property to SBT with this approach; rather, it is controlled by the javaOptions
setting, as in the example above.