问题
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
?
回答1:
run
forks but console
doesn't, so simply sbt -Dfoo=bar console
If need be you can set it:
- in the sbt
shell
witheval sys.props("foo") = "bar"
- in the REPL (
console
) withsys.props("foo") = "bar"
- in
build.sbt
withval setFoo = sys.props("foo") = "bar"
回答2:
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)
来源:https://stackoverflow.com/questions/37213752/pass-system-property-to-sbt-console