Pass System Property to `sbt console`?

假装没事ソ 提交于 2019-12-11 13:07:23

问题


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



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!