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?
sbt is a popular tool for compiling, running, and testing Scala projects of any size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies or more than one code file.
SBT is an interactive build tool that is used to run tests and package your projects as JAR files. SBT lets you create a project in a text editor and package it, so it can be run in a cloud cluster computing environment (like Databricks).
sbt is a build tool for Scala, Java, and more. It requires Java 1.8 or later.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With