设置要在REPL中访问的配置属性

时间:2015-09-15 20:45:44

标签: scala sbt typesafe-config

假设:

的src /测试/阶/净/ Main.scala

package net

import com.typesafe.config.ConfigFactory

object Main extends App {
    override def main(args: Array[String]) {
        val bar   = ConfigFactory.load().getString("app.bar")
        val bippy = ConfigFactory.load().getString("app.bippy")
        println(s"bar: $bar | bippy : $bippy")
    }
}

的src /测试/资源/ application.conf

app {
    bar = ${?BAR}
    bippy = ${?BIPPY}
}

我尝试在BAR中设置BIPPYsbt个环境变量:

>set envVars := Map("BAR" -> "bar!", "BIPPY" -> "bippy!")

然后,我在测试模式下打开了REPL:

>test:console

scala> import net.Main
import net.Main

scala> Main.main(Array())
com.typesafe.config.ConfigException$Missing: No configuration setting 
    found for key 'app.bar'

如何为REPL设置这些属性?

1 个答案:

答案 0 :(得分:1)

使用-Dconfig.file系统属性

传递配置文件
[localhost]$ sbt -Dconfig.file=src/test/resources/application.conf
[info] Loading global plugins from ~/.sbt/0.13/plugins
[info] Loading project definition from ~/my/project
[info] Set current project to my-project (in build file:~/my/project/)
> console
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.11.6 (OpenJDK 64-Bit Server VM, Java 1.8.0_72-internal).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import com.typesafe.config._
import com.typesafe.config._

scala> val config = ConfigFactory.load()
config: com.typesafe.config.Config = Config(SimpleConfigObject({"test": "success"})

scala> val value = config.getString("test")
value: String = test