S-JIS[2012-03-10/2012-05-03] 変更履歴

Play!2.0インストール

Play framework 2.0のインストールのメモ。


インストール

前提(たぶん): Scalaがインストールされていること。

  1. アーカイブをダウンロードする。[/2012-04-28]
  2. 適当な場所に展開する。(例: C:\scala\play-2.0)
  3. 展開したディレクトリーへパスを通しておく。(UNIX用のplayシェル、Windows用のplay.batがそこに入っているから)
  4. コンソールから「play help」を実行して、それっぽいもの(笑)が表示されればOK。

Playはsbtを使用するが、必要になったときに自動的にダウンロード・インストールされるらしい。


新規アプリケーションの作成

参考: 新規アプリケーションを作成する

  1. アプリケーションのディレクトリーを作りたい場所に移動する。
    > cd /d D:\temp
  2. 新規アプリケーションを作成する。
    > play new MyFirstApp
    途中で聞かれる事 備考
    What is the application name? アプリケーション名 first application
    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
    これで、「D:\temp\MyFirstApp」が作られる。
  3. 実行してみる。
    1. > cd MyFirstApp
      > play
    2. しばらく待つと、「[first application] $」 というプロンプトが表示される。
    3. ウェブサーバーを起動する。
      [first application] $ run
      〜
      [info] play - Listening for HTTP on port 9000...
      
      (Server started, use Ctrl+D to stop and go back to the console...)
    4. ブラウザーで「http://localhost:9000」を開くと、何か表示されるはず。
    5. コンソール側でCtrl+Dを押すと、サーバーは終了する。
    6. [first application] $ exit

雛形アプリケーションの内部の処理の流れ


Eclipseへのインポート

PlayのアプリケーションをEclipseへインポートする方法。

参考: 中村 修太さんのPlayframework 2.0 小技-Controllerなしでリダイレクト

  1. 自分のPlayアプリケーションのコンソールを起動する。
    > cd /d D:\temp\MyFirstApp
    > play
  2. Eclipseの設定を生成する。
    [first application] $ eclipsify
    [info] About to create Eclipse project files for your project(s).
    [info] Successfully created Eclipse project files for project(s): first application
  3. Eclipseのインポート機能を使って、MyFirstAppをインポートする。

ソース付きの設定

eclipsifyに「with-source=true」を付けると、jarファイルに対応する“ソースのjarファイル”もダウンロードされて、Eclipseからjarファイル内のクラスを参照したときにソースが見られるようになる。(ソースの添付がされた状態になる)
らしいのだが、Play2.0のplayコマンドで試してみると上手くいかなかった。[2012-04-28]

参考: Play2.0 eclipsify with-source=true

この場合、どうもソースのjarファイルをローカルのリポジトリーから探すようで、当然ダウンロードされていないから見つからないということらしい。
以下のような警告が出る。(ソースの添付はされない状態になるが、Eclipseの設定自体はちゃんと作られる)

> cd /d D:\temp\test1
D:\temp\test1> play

[test1] $ eclipsify with-source=true
〜
[warn]  [NOT FOUND ] org.easytesting#fest-util;1.1.6!fest-util.jar(src) (0ms)
[warn] ==== local: tried
[warn]   C:\scala\play-2.0\framework\..\repository/local/org.easytesting/fest-util/1.1.6/srcs/fest-util-sources.jar
[warn]  [NOT FOUND ] org.easytesting#fest-util;1.1.6!fest-util.jar(doc) (0ms)
[warn] ==== local: tried
[warn]   C:\scala\play-2.0\framework\..\repository/local/org.easytesting/fest-util/1.1.6/docs/fest-util-javadoc.jar
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  ::              FAILED DOWNLOADS            ::
[warn]  :: ^ see resolution messages for details  ^ ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: org.scala-lang#scala-library;2.9.1!scala-library.jar(src)
[warn]  :: org.scala-lang#scala-library;2.9.1!scala-library.jar(doc)
[warn]  :: play#play_2.9.1;2.0!play_2.9.1.jar(src)
[warn]  :: play#play_2.9.1;2.0!play_2.9.1.jar(doc)
[warn]  :: play#templates_2.9.1;2.0!templates_2.9.1.jar(src)
[warn]  :: play#templates_2.9.1;2.0!templates_2.9.1.jar(doc)
〜

playコマンドを使わず、sbtコマンドを直接使えば大丈夫らしい。

sbtの環境を用意していない場合は作る必要がある。
Play2.0をインストールしていればsbtのjarファイルもダウンロードされているので、jarファイルを起動するバッチファイル(Windowsの場合)だけ作ればいい。
(参考: xsbt WikiのSetup

  1. Play2.0をインストールしたディレクトリーに移動する。
  2. sbt.batというファイルを作り、中を以下の様に記述する。(sbt-launch.jarのパスはインストールした環境に応じて変更する)
    java -Xmx512M -jar "C:\scala\play-2.0\framework\sbt\sbt-launch.jar" %*
> cd /d D:\temp\test1
D:\temp\test1> sbt

[test1] $ eclipsify with-source=true

ソースのダウンロードに十数分かかるが、これで上手くいく。
.classpathファイルの中を見ると、sourcepathの設定が増えているのが分かる)


sbtコマンドを使わず、ivysettings.xmlを設定する方法もあるらしい。(→Yasushiさんのplayのeclipsify with-source=true 対策[2012-05-03]

  1. Playアプリケーションディレクトリー直下にivysettings.xmlというファイルを作る。内容は以下のようなもの。
    (ローカルリポジトリーとして、Playをインストールしたディレクトリーを絶対パスで指定する)
    (コメントを日本語で入れたい場合は、ファイルをUTF-8にする)
    <ivysettings>
      <caches useOrigin="true"/>
      <settings defaultResolver="play"/>
      <resolvers>
        <chain name="play">
          <filesystem name="1">
            <artifact pattern="C:/scala/play-2.0/repository/local/[organisation]/[module]/[revision]/[type]s/[artifact].[ext]" />
          </filesystem>
          <url name="type-ivy-release">
            <artifact pattern="http://repo.typesafe.com/typesafe/ivy-releases/[organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext]"/>
          </url>
          <ibiblio name="maven2" m2compatible="true"/>
        </chain>
      </resolvers>
    </ivysettings>
  2. Playアプリケーションディレクトリー/project/Build.scalaを修正する。
        val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
          // Add your own project settings here
          externalIvySettings()	←追加
        )
  3. > cd /d D:\temp\test1
    D:\temp\test1> sbt
    
    [test1] $ eclipsify with-source=true

再度設定をやり直したい場合は、クリーンする。

[test1] $ clean
[test1] $ eclipsify with-source=true

アプリケーションのバージョンアップ

既存Playあぷrケーションのバージョンを変えるには、plusgins.sbt内のsbt-pluginのバージョンを書き換える。(→ueshinさんのツイート[2012-05-03]

Playアプリケーションディレクトリー/project/plugins.sbt:

// Use the Play sbt plugin for Play projects
addSbtPlugin("play" % "sbt-plugin" % "2.0.1")	←バージョンの数値を2.0から2.0.1に書き換える

そして、再度playコマンドを実行すればOK。

> play

This project uses Play 2.0.1!
Update the Play sbt-plugin version to 2.0 (usually in project/plugins.sbt)

Play目次へ戻る / Scalaへ戻る / 技術メモへ戻る
メールの送信先:ひしだま