简体   繁体   English

jvm选项没有传递给分叉进程

[英]jvm options not passed on to forked process

According to the documentation, an sbt forked process should receive the jvm settings of the current process: 根据文档,sbt forked进程应该接收当前进程的jvm设置:

By default, a forked process uses the same Java and Scala versions being used for the build and the working directory and JVM options of the current process. 默认情况下,分叉进程使用与构建相同的Java和Scala版本以及当前进程的工作目录和JVM选项。 See: http://www.scala-sbt.org/0.13/docs/Forking.html 见: http//www.scala-sbt.org/0.13/docs/Forking.html

However that does not seem to be the case for me. 然而,对我来说似乎并非如此。 Take the following test: 参加以下测试:

object Test {

        def main(args: Array[String]): Unit = {

                println("Conf: " + System.getProperty("config.resource"))
        }
}

If I run this with sbt -Dconfig.resource=test.conf then "Conf: test.conf" gets printed. 如果我使用sbt -Dconfig.resource = test.conf运行它,则会打印“Conf:test.conf”。 But once I add fork in run := true in my build.scala "Conf: null" is printed out. 但是一旦我在build.scala中添加了fork:= true,就打印出“Conf:null”。 Which implies to me that the jvm options are not in fact getting passed to the forked process. 这对我来说意味着jvm选项实际上并没有传递给分叉进程。 Can someone tell me what am I missing here? 谁能告诉我在这里我错过了什么?

import sbt._
import Keys._

object Build extends Build {
        lazy val root = (project in file(".")).
        settings(
        fork in run := true
        )
}

The SBT documentation is correct, the JVM properties do get passed to the forked process. SBT文档是正确的,JVM属性确实传递给分叉进程。 You're concerned about the System properties, however, which need to be passed manually. 但是,您需要关注需要手动传递的系统属性。 Try this to pass all system properties: 尝试此操作以传递所有系统属性:

import scala.collection.JavaConversions._

javaOptions in run += {
  val props = System.getProperties
  props.stringPropertyNames().toList.map { configKey =>
    s"-D$configKey=${props.getProperty(configKey)}"
  }.mkString(" ")
}

if you ask sbt to fork the VM in which it runs your code, then it doesn't inherit the system properties of the parent VM 如果你要求sbt分叉运行代码的VM,那么它不会继承父VM的系统属性

fork in run := true

fork in console := true

javaOptions in run += s"-Dconfig.resource=${Option(System.getProperty("config.resource")).getOrElse("default")}"

javaOptions in console += s"-Dconfig.resource=${Option(System.getProperty("config.resource")).getOrElse("default")}"

This Works for me... 这对我有用......

This is what I used. 这是我用的。 It's an updated version of josephpconley 's answer. 这是josephpconley答案的更新版本。

  javaOptions ++= {
    val props = sys.props.toList
    props.map {
      case (key, value) => s"-D$key=$value"
    }
  },

When you fork JVM, you actually create new process. 分叉JVM时,实际上是在创建新进程。 Sbt will not copy JVM arguments to the new process. Sbt不会将JVM参数复制到新进程。 You have to specify those explicitly, for example: 您必须明确指定,例如:

javaOptions in test += "-Dconfig.file=app.test.conf"

When you forbid forking in test, for example: 当你在测试中禁止分叉时,例如:

fork in test := true

your tests are being run in the same JVM(with it's arguments). 你的测试是在同一个JVM中运行的(带有它的参数)。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM