Build.sbt 和 build.scala 有什么区别?

我开始学习 Scala,几乎在每个教程中我都会看到一个描述项目设置的 build.sbt文件。但是现在我已经安装了 giter8并从模板创建了一个项目。并从模板生成的项目遗漏了 build.sbt文件,但它有 build.scala(这似乎用于相同的目的,但它更灵活)。

那么 build.sbtbuild.scala有什么区别呢?
哪个更受欢迎,为什么?

25964 次浏览

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

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.

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.