seratch's weblog in Japanese

About Scala, Java and Ruby programming in Japaense. If you need English information, go to http://blog.seratch.net/

Play 2.0 Scala プロジェクトのセットアップ手順

Mac の方はこちらもどうぞ

http://d.hatena.ne.jp/seratch2/20120410/1334074688

Windows での手順

Play 2.0 Scalaの検証をしているので、最初の手順をまとめました。特に理由はないですが、Windows上で試した記録です。
Windowsでplayコマンドを起動すると「Windows, really? Ok, disabling colors.」と毎回茶化されます。

Play 2.0 Scala

まだこれからの部分もありますが、公式ドキュメントはこちらです。

http://www.playframework.org/2.0
https://github.com/playframework/play20

クラスメソッドさんのブログ

2011/11時点で試している偉大な先人です。参考になりました。

http://dev.classmethod.jp/tool/play20-beta-eclipse/

2012/03/13 に Play 2.0 Final に・・・

https://twitter.com/#!/playframework/status/179554805809819648


以上を踏まえた上でお読みください。

Playをダウンロード

zipファイルをダウンロードしてきて解凍、そのディレクトリにPATHを通しておきます。

wget http://download.playframework.org/releases/play-2.0.zip
unzip play-2.0.zip

どこでもこれができるようなればOKです。

$ play.bat

Windows, really? Ok, disabling colors.

       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/

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.

なお、Mac OS の場合は Homebrew でセットアップできます。

brew install play

Scala IDE(Eclipseプラグイン)

以下のupdate siteからEclipseプラグインをインストールします。

http://download.scala-ide.org/releases-29/stable/site

Scala IDE is officially supporting Eclipse 3.6.2 (Helios).

http://scala-ide.org/download/current.html

と言っているので、IndigoではなくHeliosにしておくのがよさそうです。
この記事での検証時もHeliosを使いました。

Scala IDEは一昔前と比較すると相当進化していますね。

プロジェクト作成

さっそく新しいPlayプロジェクトをつくります。

$ play.bat new play20-preview

Windows, really? Ok, disabling colors.

       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/

play! 2.0, http://www.playframework.org

The new application will be created in PROJECT_DIR

What is the application name?
> play20-preview

Which template do you want to use for this new application?

  1 - Create a simple Scala application
  2 - Create a simple Java application
  3 - Create an empty project

> 1

OK, application play20-preview is created.

Have fun!

[C:workspace]

sbteclipseの導入

playコンソール(sbtベース)でeclipseコマンドを叩くためにsbteclipseを追加します。

project/plugins.sbt に追記:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse" % "2.0.0")

eclipse create-src

eclipse create-src」を実行してEclipse上でリフレッシュすると、参照ライブラリが解決されます。

$ play.bat
[info] Loading project definition from PROJECT_DIR
[info] Updating {file:/PROJECT_DIR/project/}default-a8056a...
[info] Done updating.
[info] Set current project to play20-preview (in build file:/PROJECT_DIR/)

Windows, really? Ok, disabling colors.

       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/

play! 2.0, http://www.playframework.org

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

[play20-preview] $ eclipse create-src
[info] About to create an Eclipse project for you.
[info] Please hang on, because it might be necessary to perform one or more updates and this might take some time ...
[info] Successfully created Eclipse project files. Please select the appropriate Eclipse plugin for Scala 2.9.1!
[play20-preview] $ exit
Build path contains duplicate entr...'app' for project 'xxx'

「Build path contains duplicate entr...'app' for project 'play20-preview'」というようなエラーメッセージが出ていると思います。

これはプロジェクトのプロパティで「Java Build Path」の「Source」タブで全て一旦削除して、「app」「test」ディレクトリだけになるようにすると解決します。

「views.html.index」がコンパイルエラー

さらに「controllers.Application」の「views.html.index」部分がコンパイルエラー表示になっていると思います。
これはviewsのindex.scala.htmlのコンパイル結果がクラスパスにいないためです。

playコンソール上でのcompileを実行します。「*.scala.html」のコンパイルはEclipseのコンパイルでは実行されません。

target/scala-2.9.1/classes配下に.classファイルがたくさんつくられていればコンパイルできています。

この状態で、EclipseのBuild Pathに「target/scala-2.9.1/classes」をAdd Class Folderしてリフレッシュすれば解決するはずです。

サーバ起動

runコマンドでHot reloadingなサーバが起動します。

[play20-preview] $ run

--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on port 9000...

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

http://localhost:9000/ でアクセスできます。

「app/controllers/Application.scala」や「app/views/index.scala.html」を書き換えて再アクセスすると、playコンソールに「--- (RELOAD) ---」と出力され、再コンパイルが実行されます。

2012/01/18時点でrunコマンドで起動したサーバだと、html内の日本語は文字化けします。
(Hot reloading ではないプロダクション設定のサーバを起動するstartコマンドだと問題ないのですが)

チケットを登録しました。
https://play.lighthouseapp.com/projects/82401/tickets/97-in-development-mode-multibyte-characters-in-viewshtml-show-up-as-garbage-characters

2012/02/10追記:

今日時点での最新バージョンで問題が解決していました。


とりあえずここまでくれば色々とドキュメントを見ながら試すことができそうです。