§sbtコンソールの使用
Playアプリケーションの開発サイクル全体をsbtで管理できます。 sbtにはインタラクティブモード(shell
)があり、コマンドを1つずつ入力することもできます。 sbtは一度だけ起動すればよいため、インタラクティブモードの方が時間の経過とともに高速になる可能性があります。コマンドを1つずつ入力する場合、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] $
ヒント:タスクリストの最後にshellを実行することで、sbtシェルに入る前にいくつかのコマンドを起動することもできます。 例えば
$ 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
を使用してサーバーを終了すると、OSプロンプトに戻ります。
デフォルトでは、サーバーはポート9000で実行されます。カスタムポート(例:8080)を指定できます:sbt 'run 8080'
もちろん、ここでも**トリガー実行**が利用可能です。
$ sbt ~run
§ヘルプの入手
使用可能なコマンドに関する基本的なヘルプを表示するには、help
コマンドを使用します。特定のコマンドに関する情報を取得するために、特定のコマンドで使用することもできます。
[my-first-app] $ help run
このドキュメントにエラーが見つかりましたか?このページのソースコードはこちらにあります。ドキュメントガイドラインを読んだ後、プルリクエストを送信してください。質問やアドバイスがあれば、コミュニティフォーラムにアクセスして、コミュニティとの会話を始めてください。