107

I started to learn Scala and almost in every tutorial I see a build.sbt file which describes project settings. But now I have installed giter8 and created a project from template. And generated project from template missed build.sbt file, but it have build.scala (which seems used for same purposes, but it is more flexible).

So what is the difference between build.sbt and build.scala?
Which is more preferred and why?

Mark Rogers
  • 90,043
  • 18
  • 79
  • 129
MyTitle
  • 17,348
  • 50
  • 147
  • 265

3 Answers3

97

To give a brief example, this build.sbt:

name := "hello"

version := "1.0"

is a shorthand notation roughly equivalent to this project/Build.scala:

import sbt._
import Keys._

object Build extends Build {
  lazy val root = Project(id = "root", base = file(".")).settings(
    name := "hello",
    version := "1.0"      
  )
}

The .sbt file can also include vals, lazy vals, and defs (but not objects and classes).

See the SBT document called ".scala build definition", particularly the section "Relating build.sbt to Build.scala".

Consider a .scala build definition if you're doing something complicated where you want the full expressiveness of Scala.

Chris Martin
  • 28,558
  • 6
  • 66
  • 126
  • 6
    On "build.sbt rarely ends up being sufficient", it is true in 0.12 and earlier that once you want to define tasks or multi-module builds, you need .scala files. This should be addressed in 0.13, where you can now define vals and multiple projects. You are of course still welcome to start with .scala though. – Mark Harrah Aug 05 '13 at 14:39
  • 1
    SBT documentation on this topic - http://www.scala-sbt.org/release/tutorial/Full-Def.html – neowulf33 Jun 29 '14 at 08:54
32

Update July 2016 (3 years later)

Build.scala is officially deprecated in sbt 0.13.12

The Build trait is deprecated in favor of the .sbt format

PR 2530 implements that deprecation.
"Appendix: .scala build definition" has been updated.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
13

When .sbts are being compiled, they are before that sort of merged with the .scala files inside project directory. They can't be used in recursive tasks, that is, you can't customize sbt from sbt, for example. For more detailed information, consider reading related section is sbt documentation: http://www.scala-sbt.org/release/docs/Getting-Started/Basic-Def.html#sbt-vs-scala-definition

tkroman
  • 4,612
  • 1
  • 21
  • 43