Play framework 2.0 previewを試してみる

2011.09.10

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Play framework 2.0

前回のエントリーで書きましたが、Play frameworkの2.0 previewがリリースされました。 Scalaがネイティブサポートされ、ビルドシステムがSBTベースになるなど、大幅な改修が行われています。 公式サイトからPlay framework 2.0 previewがダウンロードできるので、早速試してみましょう。

ダウンロード&インストール

ここからダウンロードし、zipファイルを解凍後、PATHに追加します。 playコマンドを入力し、下記のような表示がされれば大丈夫です。

$ play
       _            _ 
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/ 
             
play! 2.0, http://www.playframework.org

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.org.

バージョンが2.0になってますね。

プロジェクトの作成とplayコンソール

まずはプロジェクトを作成してみましょう。 hello-play2という名前でプロジェクトを作成します。

$ mkdir hello-play2
$ cd hello-play2/
$ play new
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
       _            _ 
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/ 
             
play! 2.0, http://www.playframework.org

The new application will be created in /pgdev/play/projects/hello-play2
What is the application name? hello-play2

OK, application hello-play2 is created.
Type `play` to enter the development console.
Have fun!

空のディレクトリを作成し、そこでplay newとすればプロジェクトが作成されます。初回はライブラリのダウンロードが行われるかもしれません。 playコマンドの中身を見てみると、下記のようになっています。

if [ -f conf/application.yml ]
then
	`dirname $0`/framework/build play "$@"
else
	java -cp `dirname $0`/framework/sbt/boot/scala-2.9.0/lib/*:\
	`dirname $0`/framework/sbt/boot/scala-2.9.0/org.scala-tools.sbt/sbt/0.10.1/*:\
	`dirname $0`/repository/play/play_2.9.0/2.0/jars/* play.console.Console "$@"

すでにここから一部SBTを使用しているようです。 コマンドで「play」と入力すれば、playコンソールが起動します。

$ play
・・・・・・
\_ 
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/ 
             
play! 2.0, http://www.playframework.org

> Type "help" or "license" for more information.
> Type "exit" or use Ctrl+D to leave this console.

[hello-play2] $

では動かしていきましょう。

playコンソールを動かしてみる

play2.0からはこのplayコンソール上でテストやサーバー実行、パッケージング等を行っていくようです。 helpと入力すれば、コンソール上で使用できるコマンドが表示されます。

[hello-play2] $ help

Welcome to Play 2.0!

These commands are available:
-----------------------------
clean          Clean all generated files.
compile        Compile the current application.
dist           Construct standalone application package.
package        Package your application as a JAR.
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.
start          Start the current application in another JVM in PROD mode.
update         Update application dependencies.

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

生成されたアプリを実行してみましょう。runと入力すれば起動します。

[hello-play2] $ run
[info] Running the application from SBT, auto-reloading is enabled

Loading config [akka.conf] from the application classpath.
[info] Listening for HTTP on port 9000...

(Server started, use Ctrl+D to stop and go back to the console...)

[info] Updating {file:/pgdev/play/projects/hello-play2/}hello-play2...
・・・・
play2/target/scala-2.9.0.final/classes...

ブラウザでhttp://localhost:9000にアクセスすれば、Hello World画面が表示されます。 サーバーを停止してplayコンソールに戻りたい場合、Ctrl+Dで戻ります。

では最後に、プロジェクトをパッケージングしましょう。 distコマンドを使用すれば、プロジェクトをzipに圧縮してくれます。

[hello-play2] $ dist
[info] Packaging /hello-play2/target/scala-2.9.0.final/hello-play2_2.9.0-0.1.jar ...
[info] Done packaging.

Your application is ready in /pgdev/play/projects/hello-play2/dist/hello-play2-0.1.zip

[success] Total time: 1 s, completed 2011/09/10 11:14:01

このzipを適当な場所で解凍し、ディレクトリの中にあるrunコマンドを実行すれば、アプリケーションが起動します。

まとめ

今回はPlay framework 2.0 previewの概要と簡単な使い方をご紹介しました。 SBTを使用することで軽く使えてテストや依存性管理でも力を発揮してくれそうですね。 これからもPlay2.0をいろいろ試していきたいと思います。