§使用 sbt 主控台
您可以使用 sbt 管理 Play 應用程式的完整開發週期。sbt 有互動模式(shell
),或者您可以一次輸入一個指令。互動模式可以隨著時間推移而加快,因為 sbt 只需要啟動一次。當您一次輸入一個指令時,每次執行時 sbt 都會重新啟動。
§單一指令
您可以直接執行單一 sbt 指令。例如,要建置並執行 Play,請變更至專案目錄並執行
$ sbt run
您將看到類似
[info] Loading project definition from /Users/play-developer/my-first-app/project
[info] Set current project to my-first-app (in build file:/Users/play-developer/my-first-app/)
--- (Running the application from sbt, auto-reloading is enabled) ---
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Enter to stop and go back to the console...)
The application starts directly. When you quit the server using Ctrl+D or Enter, the command prompt returns.
§互動模式
若要以互動模式啟動 sbt,請切換至專案的最上層,並輸入不帶任何引數的 sbt
$ cd my-first-app
my-first-app $ sbt
您將看到類似
[info] Loading global plugins from /Users/play-developer/.sbt/1.0/plugins
[info] Loading project definition from /Users/play-developer/my-first-app/project
[info] Updating {file:/Users/play-developer/my-first-app/project/}my-first-app-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to my-first-app (in build file:/Users/play-developer/my-first-app/)
[my-first-app] $
提示:您也可以在進入 sbt shell 之前啟動一些指令,方法是在任務清單的結尾處執行 shell。例如
$ sbt clean compile shell
§開發模式
在此模式中,sbt 會啟動已啟用自動重新載入功能的 Play。當您提出要求時,如果任何檔案已變更,Play 將自動重新編譯並重新啟動您的伺服器。如有需要,應用程式將自動重新啟動。
在互動模式中使用 sbt,在開發模式中執行目前的應用程式,請使用 run
指令
[my-first-app] $ run
您將看到類似
$ sbt
[info] Loading global plugins from /Users/play-developer/.sbt/1.0/plugins
[info] Loading project definition from /Users/play-developer/tmp/my-first-app/project
[info] Done updating.
[info] Loading settings for project root from build.sbt ...
[info] Set current project to my-first-app (in build file:/Users/play-developer/tmp/my-first-app/)
[info] sbt server started at local:///Users/play-developer/.sbt/1.0/server/c9c53f40a402da68f71a/sock
[my-first-app] $ run
[info] Updating ...
[info] Done updating.
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
--- (Running the application, auto-reloading is enabled) ---
[info] p.c.s.PekkoHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Enter to stop and go back to the console...)
§僅編譯
您也可以在不執行 HTTP 伺服器的狀況下編譯您的應用程式。compile
指令會在指令視窗中顯示任何應用程式錯誤。例如,在互動模式中,請輸入
[my-first-app] $ compile
您將看到類似
[my-first-app] $ compile
[info] Compiling 1 Scala source to /Users/play-developer/my-first-app/target/scala-2.13/classes...
[error] /Users/play-developer/my-first-app/app/controllers/HomeController.scala:21: not found: value Actionx
[error] def index = Actionx { implicit request =>
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 1 s, completed Feb 6, 2017 2:00:07 PM
[my-first-app] $
如果您的程式碼沒有錯誤,您將看到
[my-first-app] $ compile
[info] Updating {file:/Users/play-developer/my-first-app/}root...
[info] Resolving jline#jline;2.12.2 ...
[info] Done updating.
[info] Compiling 8 Scala sources and 1 Java source to /Users/play-developer/my-first-app/target/scala-2.13/classes...
[success] Total time: 3 s, completed Feb 6, 2017 2:01:31 PM
[my-first-app] $
§測試選項
您可以在不執行伺服器的狀況下執行測試。例如,在互動模式中,請使用 test
指令
[my-first-app] $ test
test
指令將執行專案中的所有測試。您也可以使用 testOnly
選擇特定測試
[my-first-app] $ testOnly com.acme.SomeClassTest
§啟動 Scala 主控台
輸入 console
以進入 Scala 主控台,這讓您可以互動式地測試您的程式碼
[my-first-app] $ console
在 scala 主控台中啟動應用程式 (例如存取資料庫)
import play.api._
val env = Environment(new java.io.File("."), this.getClass.getClassLoader, Mode.Dev)
val context = ApplicationLoader.Context.create(env)
val loader = ApplicationLoader(context)
val app = loader.load(context)
Play.start(app)
§除錯
您可以在啟動主控台時要求 Play 啟動一個 JPDA 除錯埠。然後您可以使用 Java 除錯器連線。使用 sbt -jvm-debug <port>
指令執行此操作
$ sbt -jvm-debug 9999
當 JPDA 埠可用時,JVM 會在開機期間記錄此列
Listening for transport dt_socket at address: 9999
§使用 sbt 功能
您可以使用 sbt 功能,例如 觸發執行。
例如,使用 ~ compile
[my-first-app] $ ~ compile
每次變更原始檔時,都會觸發編譯。
如果您使用 ~ run
[my-first-app] $ ~ run
當開發伺服器正在執行時,將會啟用觸發編譯。
您也可以對 ~ test
執行相同的操作,以便每次修改原始檔時持續測試您的專案
[my-first-app] $ ~ test
如果您想使用 testOnly
指令只執行一小組測試,這可能會特別有用。例如
[my-first-app] $ ~ testOnly com.acme.SomeClassTest
每次修改原始檔時,將會觸發執行 com.acme.SomeClassTest
測試。
§直接使用 play 指令
您也可以直接執行指令,而不用進入 Play 主控台。例如,輸入 sbt run
$ sbt run
[info] Loading project definition from /Users/play-developer/my-first-app/project
[info] Set current project to my-first-app (in build file:/Users/play-developer/my-first-app/)
--- (Running the application from sbt, auto-reloading is enabled) ---
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Enter to stop and go back to the console...)
應用程式會直接啟動。當您使用 Ctrl+D
或 Enter
退出伺服器時,您將會回到您的作業系統提示字元。
預設情況下,伺服器會在 9000 埠上執行。可以指定自訂埠 (例如 8080):sbt 'run 8080'
當然,觸發執行 在這裡也可以使用
$ sbt ~run
§取得協助
使用 help
指令取得可用指令的基本說明。您也可以搭配特定指令使用此指令,取得該指令的資訊
[my-first-app] $ help run
下一步:設定您偏好的 IDE
在此文件發現錯誤?此頁面的原始程式碼可以在 這裡 找到。閱讀完 文件指南 後,請隨時提交拉取請求。有問題或建議要分享?請前往 我們的社群論壇 與社群展開對話。