auramo
auramo

Reputation: 13357

Setting system properties with "sbt run"

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 as follows:

sbt -Dmyprop=x run

How could I do that?

Upvotes: 20

Views: 14637

Answers (5)

veysiertekin
veysiertekin

Reputation: 1871

You can pass system properties at the end of the sbt command:

sbt run -Dmyprop=x

If you have to pass program parameters into a stage, just pass system properties after the quotes again:

sbt "runMain com.example.MyClass -p param-value" -Dmyprop=x

Upvotes: 1

yerlilbilgin
yerlilbilgin

Reputation: 3409

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

Upvotes: 0

Marco Massenzio
Marco Massenzio

Reputation: 3002

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

Upvotes: 5

akauppi
akauppi

Reputation: 18046

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

Upvotes: 10

Shaun the Sheep
Shaun the Sheep

Reputation: 22742

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.

Upvotes: 17

Related Questions