I\'m using a pretty recent version of SBT (seems to be hard to figure out what the version is). I want to pass system properties to my application with sbt run
I think the best is to use the JAVA_OPTS environment variable:
#update the java options (maybe to keep previous options)
export JAVA_OPTS="${JAVA_OPTS} -Dmyprop=x"
#now run without any extra option
sbt run
Thanks for the pointer, this actually helped me solve a somewhat related problem with Scala Tests.
It turned out that sbt
does fork the tests when there are sub-projects (see my code) and some of the tests fail to pick up the system property.
So in sbt -Dsomething="some value" test
, some of the tests would fail when failing to find something
in the system properties (that happened to be my DB URI, so it kinda mattered!)
This was driving me nuts, so I thought I'd post it here for future reference for others (as @akauppi correctly noted, chances are high that "others" may well be me in a few weeks!).
The fix was to add the following to build.st
:
fork in Test := false
I found the best way to be adding this to build.sbt
:
// important to use ~= so that any other initializations aren't dropped
// the _ discards the meaningless () value previously assigned to 'initialize'
initialize ~= { _ =>
System.setProperty( "config.file", "debug.conf" )
}
Related: When doing this to change the Typesafe Configuration that gets loaded (my use case), one needs to also manually include the default config. For this, the Typesafe Configuration's suggested include "application"
wasn't enough but include classpath("application.conf")
worked. Thought to mention since some others may well be wanting to override system properties for precisely the same reason.
Source: discussion on the sbt mailing list
SBT's runner doesn't normally create new processes, so you also have to tell it to do this if you want to set the arguments that are passed. You can add something like this to your build settings:
fork := true
javaOptions := Seq("-Dmx=1024M")
There's more detail on forking processes in the SBT documentation.