Jvm parameters are not passed to the forked process

According to the documentation, the sbt forked process should get the jvm settings of the current process:

By default, the forked process uses the same Java and Scala versions that are used to build and the working directory and JVM parameters of the current process. See: http://www.scala-sbt.org/0.13/docs/Forking.html

However, this does not seem to be the case. 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, "Conf: test.conf" will print. But as soon as I add fork to run: = true in my build.scala, "Conf: null" is printed. This implies that jvm options are not actually passed to the forked process. Can someone tell me what I'm missing here?

 import sbt._ import Keys._ object Build extends Build { lazy val root = (project in file(".")). settings( fork in run := true ) } 
+8
scala jvm sbt
source share
5 answers

The SBT documentation is correct, the JVM properties are passed to the forked process. However, you are worried about the properties of the system that need to be transferred manually. Try to transfer all the 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(" ") } 
+6
source share

if you ask sbt to fork the virtual machine in which it runs your code, then it does not inherit the system properties of the parent 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 ...

+3
source share

When you create a JVM, you are actually creating a new process. Sbt will not copy JVM arguments to the new process. You must specify them explicitly, for example:

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

If you disable forking in the test, for example:

 fork in test := true 

your tests run in the same JVM (with its arguments). Hope this helps.

+1
source share

From the sbt documentation you can use javaOptions in (Test, run):

 Forked JVM optionsΒΆ To specify options to be provided to the forked JVM, set javaOptions: javaOptions in run += "-Xmx8G" or specify the configuration to affect only the main or test run tasks: javaOptions in (Test,run) += "-Xmx8G" or only affect the test tasks: javaOptions in test += "-Xmx8G" 

http://www.scala-sbt.org/0.12.3/docs/Detailed-Topics/Forking.html

0
source share

This is what I used. This is an updated version of josephpconley answer.

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

All Articles