Kevin Meredith
Kevin Meredith

Reputation: 41919

Setting configuration properties to access in REPL

Given:

src/test/scala/net/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/test/resources/application.conf

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

I attempted to set the BAR and BIPPY environment variables in sbt:

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

Then, I opened the REPL in test mode:

>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'

How can I set these properties for the REPL?

Upvotes: 1

Views: 552

Answers (1)

Shane Perry
Shane Perry

Reputation: 990

Pass your configuration file using the -Dconfig.file system property

[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

Upvotes: 1

Related Questions