user79074
user79074

Reputation: 5270

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:

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. See: 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. But once I add fork in run := true in my build.scala "Conf: null" is printed out. Which implies to me that the jvm options are not in fact getting passed to the forked process. 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
        )
}

Upvotes: 11

Views: 4794

Answers (4)

insan-e
insan-e

Reputation: 3921

When you fork JVM, you actually create new process. Sbt will not copy JVM arguments to the new process. 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 := false

your tests are being run in the same JVM(with it's arguments). Hope this helps.

Upvotes: 2

RainbowProgramming
RainbowProgramming

Reputation: 67

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

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

Upvotes: 5

josephpconley
josephpconley

Reputation: 1723

The SBT documentation is correct, the JVM properties do get passed to the forked process. 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(" ")
}

Upvotes: 9

Atiq
Atiq

Reputation: 396

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

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

Upvotes: 4

Related Questions