13

I'm sure I'm missing something really simple... I want to set the system property java.awt.headless to true for my sbt project. Reading the page on properties I think that I need to use system or systemOptional. In my project file I've tried things like:

lazy val javaAwtHeadless = system[Boolean]("java.awt.headless")

Setting it as a user property (e.g. lazy val javaAwtHeadless = property[Boolean]) and setting the accompanying value in build.properties made the property visible in the sbt console but not within sbt's Scala console (via System.getProperty("java.awt.headless")).

set java.awt.headless true from the sbt console works, including being set in the Scala console, but it doesn't persist to the next time I launch sbt.

pr1001
  • 21,727
  • 17
  • 79
  • 125

3 Answers3

14

A straightforward method would be to edit the batch file or shell script that you use to run sbt and add -Dprop=val

retronym
  • 54,768
  • 12
  • 155
  • 168
  • 3
    Yes, but this would require me to change my sbt launch commands on all the machines I develop on, plus would impact other projects I develop using sbt. – pr1001 Jun 28 '10 at 10:02
  • I personally check the sbt launcher and sbt shell script into each project, for example: http://github.com/scalaz/scalaz. Have you just tried `System.setProperty("prop", "val")` somewhere in your project definition? Remember that SBT by default runs everything in the same process, unless you explicitly ask to to fork: http://code.google.com/p/simple-build-tool/wiki/Forking – retronym Jun 28 '10 at 13:48
  • Or in trunk, system.props("prop") = "val". Aaaahh, that's better. – psp Jan 08 '11 at 22:21
  • You can do that through a plugin within the shell. I wrote https://github.com/mustaghattack/jvm-command that does it. – Bruno Bieth Jul 16 '13 at 11:49
  • Or, plugin free, just `sbt> eval System.setProperty("foo", "bar")` – retronym Aug 28 '13 at 10:19
8

If I needed this option for all sbt tasks, I'd set it as follows in build.sbt

javaOptions += "-Djava.awt.headless=true" 

If it was just for one task, eg: run, you can scope that:

javaOptions in Runtime += "-Djava.awt.headless=true" 
jazmit
  • 5,170
  • 1
  • 29
  • 36
6

If you're trying to set SBT properties, like plugin settings, then the following worked for me with 0.13+. The following however did work, when trying to pass in Liquibase settings, like password, from our CI frameworks.

In your build.sbt

Ugly, but supplies defaults, and optionally grabs from System.properties. This way you've got your default and override cases covered.

def sysPropOrDefault(propName:String,default:String):String = Option(System.getProperty(propName)).getOrElse(default)

liquibaseUsername := sysPropOrDefault("liquibase.username","change_me")
liquibasePassword := sysPropOrDefault("liquibase.password","chuck(\)orris")

From the commandline

Now just override via -Dprop=value like you would with Maven or other JVM programs. Note props appear before SBT task.

sbt -Dliquibase.password="shh" -Dliquibase.username="bob" liquibase:liquibase-update

Joseph Lust
  • 19,340
  • 7
  • 85
  • 83