1. 使用 Def.sequential 定義循序任務

使用 Def.sequential 定義循序任務 

sbt 0.13.8 新增了 Def.sequential 函數,以在半循序語義下執行任務。為了示範循序任務,讓我們建立一個名為 compilecheck 的自訂任務,該任務會執行 Compile / compile,然後執行由 scalastyle-sbt-plugin 新增的 Compile / scalastyle 任務。

以下是如何設定:

project/build.properties 

sbt.version=1.9.8

project/style.sbt 

addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")

build.sbt 

lazy val compilecheck = taskKey[Unit]("compile and then scalastyle")

lazy val root = (project in file("."))
  .settings(
    Compile / compilecheck := Def.sequential(
      Compile / compile,
      (Compile / scalastyle).toTask("")
    ).value
  )

若要呼叫此任務,請從 Shell 輸入 compilecheck。如果編譯失敗,compilecheck 會停止執行。

root> compilecheck
[info] Compiling 1 Scala source to /Users/x/proj/target/scala-2.10/classes...
[error] /Users/x/proj/src/main/scala/Foo.scala:3: Unmatched closing brace '}' ignored here
[error] }
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

看起來我們能夠循序執行這些任務。