§sbt 食譜
§連結到 Play 的開發模式
當 Play 在開發模式下執行,也就是使用 sbt run
時,通常會連結到此模式以啟動開發所需的額外程序。這可透過定義 PlayRunHook
來完成,這是一個具有下列方法的特質
beforeStarted(): Unit
- 在 Play 應用程式啟動之前呼叫,但在所有「執行前」任務完成之後。afterStarted(): Unit
- 在 Play 應用程式啟動之後呼叫。afterStopped(): Unit
- 在 Play 程序停止後呼叫。
現在假設您要在應用程式啟動之前使用 grunt
建立 Web 應用程式。首先,您需要在 project/
目錄中建立一個 Scala 物件來延伸 PlayRunHook
。我們將其命名為 Grunt.scala
import play.sbt.PlayRunHook
import sbt._
import scala.sys.process.Process
object Grunt {
def apply(base: File): PlayRunHook = {
object GruntProcess extends PlayRunHook {
override def beforeStarted(): Unit = {
Process("grunt dist", base).run
}
}
GruntProcess
}
}
現在您可以在 build.sbt
中註冊此掛鉤
PlayKeys.playRunHooks += Grunt(baseDirectory.value)
每當您執行 sbt run
時,這將在應用程式啟動之前在 baseDirectory
中執行 grunt dist
指令。
現在我們要修改執行掛鉤以執行 grunt watch
指令來觀察變更,並在變更發生時重新建置 Web 應用程式,因此我們將修改之前建立的 Grunt.scala
檔案
import play.sbt.PlayRunHook
import sbt._
import java.net.InetSocketAddress
import scala.sys.process.Process
object Grunt {
def apply(base: File): PlayRunHook = {
object GruntProcess extends PlayRunHook {
var watchProcess: Option[Process] = None
override def beforeStarted(): Unit = {
Process("grunt dist", base).run
}
override def afterStarted(): Unit = {
watchProcess = Some(Process("grunt watch", base).run)
}
override def afterStopped(): Unit = {
watchProcess.map(p => p.destroy())
watchProcess = None
}
}
GruntProcess
}
}
現在,當使用 sbt run
啟動應用程式時,將執行 grunt watch
,以便在檔案變更時重新執行 grunt 建置。
§新增編譯器選項
例如,您可能想要新增功能標記以取得功能警告的詳細資料
[info] Compiling 1 Scala source to ~/target/scala-2.10/classes...
[warn] there were 1 feature warnings; re-run with -feature for details
只要將 -feature
新增至 scalacOptions
屬性即可
scalacOptions += "-feature"
§新增其他資產目錄
例如,您可以新增 pictures
資料夾作為其他資產目錄
Assets / unmanagedResourceDirectories += baseDirectory.value / "pictures"
這將允許您使用 routes.Assets.at
與此資料夾。
§設定外部化資源
自 Play 2.4 起,conf
目錄的內容預設會新增至類別路徑。
當 封裝 Play 應用程式以供生產 時,conf
資料夾(或其內容)可以在兩個地方存在conf
資料夾不會封裝至 jar
檔案(與應用程式的其他部分一起封裝),而是保留在檔案系統的 jar
檔案之外。因此,該 conf
資料夾的內容(例如 application.conf
)可以編輯,且重新啟動應用程式後,任何變更都會立即套用 - 無需重新封裝和重新部署應用程式。這是預設值。
或者,可以設定 Play,將 conf
資料夾的內容置於應用程式 jar
檔案內部。這可以透過設定
PlayKeys.externalizeResources := false
在 build.sbt
中進行。有時,當函式庫需要 conf
資料夾的(部分)資源,且應用程式類別檔案需要存在於同一個 jar
檔案中才能正常運作時,需要這種行為。
自 Play 2.7 起,存在另一個組態金鑰,可讓您排除特定資源,即使在 PlayKeys.externalizeResources := true
時,這些資源也不會被外部化
PlayKeys.externalizeResourcesExcludes += baseDirectory.value / "conf" / "somefolder" / "somefile.xml"
有了這個組態金鑰,當僅需要某些特定檔案時,您不必再將 conf
資料夾的所有檔案都放入 jar
檔案中。
§停用文件
若要加快編譯速度,您可以停用文件產生
Compile / doc / sources := Seq.empty
Compile / packageDoc / publishArtifact := false
第一行會停用文件產生,第二行會避免發佈文件成品。
§組態 ivy 記錄層級
預設情況下,ivyLoggingLevel
設定為 UpdateLogging.DownloadOnly
。您可以使用下列方式變更此值
UpdateLogging.Quiet
僅顯示錯誤UpdateLogging.Full
記錄最多
例如,如果您只想顯示錯誤
ivyLoggingLevel := UpdateLogging.Quiet
§測試中的分岔和並行執行
預設情況下,並行執行已停用,而分岔已啟用。您可以透過設定 Test / parallelExecution
和/或 Test / fork
來變更此行為
Test / parallelExecution := true
Test / fork := false
下一頁: 除錯您的建置
在這個文件中發現錯誤?此頁面的原始碼可以在 這裡 找到。在閱讀 文件指南 後,請隨時提出拉取請求。有問題或建議要分享嗎?前往 我們的社群論壇 與社群展開對話。