Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting system properties with "sbt run"

Tags:

sbt

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?

like image 558
auramo Avatar asked Apr 14 '12 17:04

auramo


People also ask

What does sbt run do?

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.

What is sbt console?

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

Which Java version does sbt use?

sbt is a build tool for Scala, Java, and more. It requires Java 1.8 or later.


2 Answers

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.

like image 60
Shaun the Sheep Avatar answered Nov 15 '22 01:11

Shaun the Sheep


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

like image 35
akauppi Avatar answered Nov 15 '22 00:11

akauppi