Play 2.0でHerokuにデプロイして開発環境を整えるまでのメモ その1

Scalaでwebアプリ書くならPlayだよねー、PlayならHerokuだよねーってことでPlayからHerokuを使う場合の作業メモ。

環境は以下の通り。

  • play 2.0.4
  • scala 2.9.2
  • sbt 0.12.1

Playのプロジェクトを作成する

ここDocumentation: Installing — Playframework からPlayのバイナリを落としてきてpathを通しておきます。
macならhomebrewからも落とせるのでそっちのほうが楽。
play helpコマンドで以下のように無事かわいらしいAAが見れたらOK
f:id:takashabe:20121106020408p:plain


次は play new myApplicationName でアプリケーションを作る。
途中で作成するテンプレが聞かれるが質問の通り1で作る。

λ ~/work/sandbox/ → play new kome  

〜略〜

What is the application name? 
> kome

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 kome is created.

Have fun!

λ ~/work/sandbox/ → 
λ ~/work/sandbox/ → 


これで最低限動くものが出来ているのでとりあえずplay runして動かしてみる。

λ ~/work/sandbox/ → cd kome 
λ ~/work/sandbox/kome/ → play run
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Loading project definition from /Users/takashabe/work/sandbox/kome/project
[info] Set current project to kome (in build file:/Users/takashabe/work/sandbox/kome/)

--- (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...)


ブラウザから localhost:9000 にアクセスして確認してみる。
f:id:takashabe:20121106022317p:plain
なんか出てきました。やりました。
ちなみにデフォルトだと9000番ポートで走るけど、terminalでplayとだけ叩いてplay consoleに入った後にrun [port] でポート番号指定して走らせることが出来る。

λ ~/work/sandbox/kome/ → 
λ ~/work/sandbox/kome/ → play    
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Loading project definition from /Users/takashabe/work/sandbox/kome/project
[info] Set current project to kome (in build file:/Users/takashabe/work/sandbox/kome/)
       _            _ 
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/ 
             
play! 2.0.4, http://www.playframework.org

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

[kome] $ run 8080

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

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

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

また、ここでもしsbtのglobal pluginなどにmpeltonen/sbt-ideaを設定している場合、名前が衝突してbuildに失敗することがある。その代わり、ideaのプロジェクトを生成するときはplay consoleから idea とだけ打てばOK。
詳細はこのpull requestなどを Fix namespace collisions with original mpeltonen version of sbt-idea by rtyley · Pull Request #1 · typesafehub/sbt-idea · GitHub

λ ~/.sbt/plugins/ → 
λ ~/.sbt/plugins/ → cat build.sbt 
resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.1.0")
λ ~/.sbt/plugins/ → 
λ ~/.sbt/plugins/ → cd -0 
~/work/sandbox/kome
λ ~/work/sandbox/kome/ → play
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Loading project definition from /Users/takashabe/work/sandbox/kome/project
[error] java.lang.NoSuchMethodError: org.sbtidea.SbtIdeaPlugin$.ideaSettings()Lscala/collection/Seq;
[error] Use 'last' for the full log.
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?


タイトルにherokuにデプロイとか書いちゃったけど、眠いのでheroku編は後日上げることにする。