Pass System Property to `sbt console`?
Asked Answered
A

2

2

Borrowing from this helpful answer, I tried to pass -Dfoo=bar to sbt console.

Given an SBT project having only a build.sbt:

$cat build.sbt
scalaVersion := "2.11.8"

fork := true

I attempted:

$sbt '; set javaOptions += "-Dfoo=bar" ; console'

scala> sys.props.get("foo")
res0: Option[String] = None

but, I had expected Some("bar") rather than None given the set ... argument.

However, using sbt ... run worked as expected:

$cat src/main/scala/net/Main.scala 
package net

object Main {
       def main(args: Array[String]): Unit = 
           println("sys.props.get('foo'): " + sys.props.get("foo"))
}

$sbt '; set javaOptions += "-Dfoo=bar" ; run'
[info] Running net.Main 
[info] sys.props.get('foo'): Some(bar)

How can I pass foo=bar as a System Property to the console?

Assembler answered 13/5, 2016 at 15:20 Comment(0)
W
9

run forks but console doesn't, so simply sbt -Dfoo=bar console

If need be you can set it:

  • in the sbt shell with eval sys.props("foo") = "bar"
  • in the REPL (console) with sys.props("foo") = "bar"
  • in build.sbt with val setFoo = sys.props("foo") = "bar"
Woo answered 13/5, 2016 at 15:42 Comment(0)
G
1

I can get system properties using the console with the following:

sbt console -Dturkey=fried

scala> sys.props.get("turkey")
res1: Option[String] = Some(fried)
Granulate answered 13/5, 2016 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.