ScalaでPlay2を試してみる。

ScalaのWebアプリケーションフレームワークには、LiftとPlay2があるが、今回Play2を試してみた。数日前にplay-2.1.3をインストールしたのだが先ほど改めてサイトをみたらplay-2.2.0なるものが登場していた。手順の再確認を兼ねて、play-2.2.0で確かめてみる。

(1) Play2のインストール

http://www.playframework-ja.org/ から play-2.2.0.zip をダウンロードして、展開する。私の環境では C:\pleiades\play-2.2.0 にしてみた。PATHにC:\pleiades\play-2.2.0を追加しておく。cmdコンソールからplayを実行する。sbtが動作して必要なファイルをretrieveしてくれる。初回は数分かかる。この時点では「This is not a play application!」と表示されて終わる。


> play
Getting org.fusesource.jansi jansi 1.11 ...

retrieving :: org.scala-sbt#boot-jansi

confs: [default]
1 artifacts copied, 0 already retrieved (111kB/75ms)
... (snip)
play 2.2.0 built with Scala 2.10.2 (running Java 1.7.0_25), http://www.playframework.com

This is not a play application!

Use `play new` to create a new Play application in the current directory,
or go to an existing application and launch the development console using `play`.

You can also browse the complete documentation at http://www.playframework.com.
>

ネット情報などを参考にプロジェクト(myPlayApp)を作成し、そのプロジェクトをeclipseに取り込めるようにしてみよう。play new myPlayApp を実行すると、myPlayAppのディレクトリが作成される。そこに cd して、再度 play を実行する。すると、プロジェクト名をプロンプトにしたplayのコンソールが動き出す。help playでコマンドを確認してみよう。compile,run,start,eclipseなどのコマンドがあることが分かる。exitでコンソールを抜けるようだ。eclipseコマンドで、myPlayAppプロジェクトをeclipseから読み込める形式に変換してくれるようだ。

> play new myPlayApp

... (snip)

The new application will be created in C:\pleiades\play-2.2.0\myPlayApp

What is the application name? [myPlayApp]
> myPlayApp // 名前を入力
Which template do you want to use for this new application?
1 - Create a simple Scala application
2 - Create a simple Java application
> 1 // 1を選択
OK, application myPlayApp is created.

Have fun!
> cd myPlayApp // 生成されたプロジェクトディレクトリに移動
> play // そこでplayを実行
Getting org.scala-sbt sbt 0.12.2 ...

retrieving :: org.scala-sbt#boot-app

confs: [default]
40 artifacts copied, 0 already retrieved (8381kB/2550ms)
[info] Loading project definition from C:\pleiades\play-2.1.3\myPlayApp\project
[info] Set current project to nengou (in build file:/C:/\pleiades/play-2.1.3/myPlayApp/)
... (snip)

[myPlayApp] $ // プロンプトが変わる
[myPlayApp] $ help play // コマンド一覧を見てみる
Welcome to Play 2.2.0!
These commands are available:

                                                        • -

classpath Display the project classpath.
clean Clean all generated files.
compile Compile the current application.
console Launch the interactive Scala console (use :quit to exit).
dependencies Display the dependencies summary.
dist Construct standalone application package.
exit Exit the console.
h2-browser Launch the H2 Web browser.
license Display licensing informations.
package Package your application as a JAR.
play-version Display the Play version.
publish Publish your application in a remote repository.
publish-local Publish your application in the local repository.
reload Reload the current application build file.
run Run the current application in DEV mode.
test Run Junit tests and/or Specs from the command line
eclipse generate eclipse project file
idea generate Intellij IDEA project file
sh execute a shell command
start Start the current application in another JVM in PROD mode.
update Update application dependencies.
...
[myPlayApp] $ eclipse // プロジェクトをEclipse用に変換

[info] Resolving org.scala-lang#scala-library;2.10.2 ...
[info] Resolving com.typesafe.play#play-jdbc_2.10;2.2.0 ...
[info] Resolving com.typesafe.play#play_2.10;2.2.0 ...
 ... (snip)
[info] Done updating.
[info] Compiling 5 Scala sources and 1 Java source to C:\pleiades\play-2.2.0\myPlayApp\target\scala-2.10\classes...
[info] Successfully created Eclipse project files for project(s):
[info] myPlayApp

[myPlayApp] $ exit // playを終了
>

(2) Play2の動作確認

これでEclipseから扱えるプロジェクトに変換されたはずだ。コマンドをみると、Intellij IDEA用のコマンド「idea」も用意されているようだ。次は、eclipseからmyPlayAppプロジェクトを読み込んでみる。その前にmyPlayAppの動作を確認してみよう。


> play
[myPlayApp] $ run

(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 Ctrl+D to stop and go back to the console...)

ブラウザでURLに http://localhost:9000/ を指定すると「Your new application is ready.」のページが開く。一旦、Ctrl+Dでplayを終了させて、私は、myPlayAppのプロジェクトディレクトリをEclipseのworkspaceの下に移動させた。playのバージョンが変わるごとに面倒かもしれないので。

(3) Eclipseへのプロジェクトの取り込み

Eclipseを起動し、先ほど作成してworkspaceに移動したプロジェクトをインポートする。「ファイル --> インポート --> 既存プロジェクトをワークスペースへ」を選択して、場所を指定する。私の場合、C:\pleiades\workspace\myPlayApp だ。以下のような構成になっていた。


myPlayApp
|
+-- app/
| +-- controllers/
| | +-- Application.scala // 開いてみよう!
| +-- views/
| +-- index.scala.html // 開いてみよう!
| +-- main.scala.html // 開いてみよう!
+-- conf/
| +-- application.conf
| +-- routes
+-- logs/
+-- project/
| +-- project/
| +-- target/
| +-- build.properties
| +-- Build.scala
| +-- plugins.sbt
+-- public/
+-- target/
| +-- native_libraries/
| +-- resolution-cache/
| +-- scala-2.10/
| +-- streams/
+-- test/
| +-- ApplicationSpec.scala // 開いてみよう!
| +-- IntegrationSpec.scala // 開いてみよう!
+-- build.sbt // 開いてみよう!
+-- README
build.sbtも用意されているし、Specs2で書かれたテストコードもあるようだ。cmdを開いて、sbtとたたくとsbtコンソールになるかと思えば、いきなりplayコンソールになる。ついでだからtestを試してみた。ApplicationSpec.scalaとIntegrationSpec.scalaが動作していることが分かる。大量のjarをretrieveしているようで初回は結構時間がかかった。testの結果、勿論エラーはないようだ。

> sbt
> set SCRIPT_DIR=C:\scala\bin\
> java -Xmx512M -Xss1M -jar "C:\scala\bin\sbt-launch.jar"
[info] Loading project definition from C:\pleiades\workspace\myPlayApp\project
[info] Set current project to myPlayApp (in build file:/C:/pleiades/workspace/myPlayApp/)
[myPlayApp] $
[myPlayApp] $ test
... (snip)

[info] Done updating.
[info] Compiling 5 Scala sources and 1 Java source to C:\pleiades\workspace\myPlayApp\target\scala-2.10\classes...
[info] Compiling 2 Scala sources to C:\pleiades\workspace\myPlayApp\target\scala-2.10\test-classes...

[info] ApplicationSpec // test/ApplicationSpec.scalaがチェックされている。
[info] Application should
[info] + send 404 on a bad request
[info] + render the index page
[info] Total for specification ApplicationSpec
[info] Finished in 2 seconds, 197 ms
[info] 2 examples, 0 failure, 0 error
[info] IntegrationSpec // test/IntegrationSpec.scalaがチェックされている。
[info] Application should
[info] + work from within a browser
[info] Total for specification IntegrationSpec
[info] Finished in 2 seconds, 703 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[success] Total time: 10 s, completed 2013/09/20 15:54:03
[myPlayApp] $

再度、runしてみる。ブラウザでURLに http://localhost:9000/ を確認する。先ほどと同じ「Your new application is ready.」のページが開く。

[myPlayApp] $ run
(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 Ctrl+D to stop and go back to the console...)

(4) Eclipse上での編集

今度は、app/controllers/Application.scalaを、たとえば以下のようにEclipse上で修正して、保存する。


package controllers
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application の準備ができた。"))
}
}
後はブラウザ上で表示のページをリロードするだけだ。誰かが勝手にcompileしてuploadしてくれている。「Your new application の準備ができた。」に変わって表示される。実行中のPlayのコンソールをみると、いつのまにかログが追加されている。

[info] play - Application started (Dev)
[info] Compiling 1 Scala source to C:\pleiades\workspace\myPlayApp\target\scala-2.10\classes...
(RELOAD)

[info] play - Application started (Dev)

playがやってくれていたのだ。重いけど機能していることは確認できた。ところで build.sbt には以下のように記述されている。specs2の記述がないのにさきほどtestは動作していた。jdbc,anorm,cacheのどれかから参照されているのだろうか。各ライブラリの依存関係をある程度認識しておかないと、検討がつかないな。依存関係をcall-graphっぽく見せてくれるコマンドなどはあるのだろうか。

name := "myPlayApp"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
jdbc,
anorm,
cache
)

play.Project.playScalaSettings

【追記 - 2013/09/22】依存関係の表示には、playコマンドのdependencies、またプラグインのsbt-dependency-graphなどが有効との情報をいただきました。またspecs2の参照は、jdbc,anorm,cacheからではなく、play.Project.playScalaSettingsの中から辿られるということです。コメントを参照のこと。また、playコマンドでは、startも使われるコマンドであると後から認識しました。