Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jvm options not passed on to forked process

Tags:

jvm

scala

sbt

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
        )
}
like image 941
user79074 Avatar asked Dec 15 '14 14:12

user79074


2 Answers

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"
    }
  },
like image 106
RainbowProgramming Avatar answered Oct 11 '22 02:10

RainbowProgramming


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(" ")
}
like image 37
josephpconley Avatar answered Oct 11 '22 01:10

josephpconley