SBT build.sbt configuration examples from the ScalaFX project

I tend to be a collector of SBT build.sbt examples, and to that end, here’s a build.sbt example from the ScalaFX project:

// Name of the project
name := "ScalaFX Hello World"

// Project version
version := "14-R19"

// Version of Scala used by the project
scalaVersion := "2.13.2"

// Add dependency on ScalaFX library
libraryDependencies += "org.scalafx" %% "scalafx" % "14-R19"
resolvers += Resolver.sonatypeRepo("snapshots")

scalacOptions ++= Seq("-unchecked", "-deprecation", "-Xcheckinit", "-encoding", "utf8", "-feature")

// Fork a new JVM for 'run' and 'test:run', to avoid JavaFX double initialization problems
fork := true

// Determine OS version of JavaFX binaries
lazy val osName = System.getProperty("os.name") match {
    case n if n.startsWith("Linux") => "linux"
    case n if n.startsWith("Mac") => "mac"
    case n if n.startsWith("Windows") => "win"
    case _ => throw new Exception("Unknown platform!")
}

// Add JavaFX dependencies
lazy val javaFXModules = Seq("base", "controls", "fxml", "graphics", "media", "swing", "web")
libraryDependencies ++= javaFXModules.map( m=>
    "org.openjfx" % s"javafx-$m" % "14.0.1" classifier osName
)

Some nice things about this SBT example are:

  • Showing how to set a resolver
  • Showing how to set scalac compiler options with scalacOptions
  • Showing how to fork a new JVM to run ScalaFX/JavaFX apps in SBT
  • The code that shows how to determine the operating system in SBT
  • The code that shows how to add the JavaFX module dependencies

Many thanks to the authors of that project for these build.sbt examples.