たなかこういちの資料室

システム開発に携わる筆者があれこれ試したことや学んだことについてのまとめ

Scala IDE for Eclipse 4.0とPlay Framework 2.3の導入記録

Scala IDE for Eclipse 4.0とPlay Framework 2.3をMac OS Xへ導入したときの記録です。導入作業は2015年3月17日〜4月1日に実施しました。
 
前提環境について(JDKの導入)
 
OS X 10.10 Yosemiteにアップグレードすると、Mountain LionなどでプリインストールされていたJDKが削除されます。Yosemiteでは、Oracleのページからダウンロードして改めて導入する必要があります。
 
 
この機会にJava 8を導入することとしました。導入実施時点の最新である「Java SE 8 update 40」を導入しました。
 
Scala IDE for Eclipseの導入
 
下記ページに、Eclipse本体とScala IDEその他一式が含まれるall-in-oneパッケージ(※ページでは「bundle」と表現されています。この記事でも以降「bundle」と表記します。)が用意されています。ここではこのbundleを素直に導入することとしました。
 

f:id:tanakakoichi9230:20150402020215p:image

導入実施時点で、バージョンは「4.0.0」となっていました。含まれるものは下記の通り。(※上記ページより引用)
 
Eclipse 4.4.2 (Luna)
Scala IDE 4.0.0
Sbt 0.13.6
Scala Worksheet 0.2.6
Play Framework support 0.4.6
ScalaTest support 2.9.3
m2eclipse-scala Maven connector 0.4.3
access to the full Scala IDE ecosystem
 
ダウンロードしたファイルのファイル名は下記の通り。
 
"scala-SDK-4.0.0-vfinal-2.11-macosx.cocoa.x86_64.zip"
 
ダウンロードした.zipを展開すると、構成済みのEclipseのパッケージになっていたので、Macの「アプリケーション」フォルダーに配置しました。

f:id:tanakakoichi9230:20150402020002p:image

Macには旧版のEclipseが導入済みでしたが、bundleに含まれるのが最新の「Luna」でしたので、既存の導入済みEclipseを削除してから、今回ダウンロードしたパッケージを新たに配置しました。
 
※アイコンが、Eclipseの標準的なものとは異なり、Scala IDEのシンボルである巻貝のようなデザインのものとなっています。
 
以上で導入は終了です。JDK 8上での起動を確認できました
 
Play Frameworkの導入
 
Scala IDEのTutorialの下記ページにPlay Frameworkの導入と利用方法の記載があるのですが、導入実施時点で、Scala IDE 3.0、Play Framework 2.1を前提とした記述のままでした。
 
"Setup and use Play framework 2.1 in Scala IDE 3.0":http://scala-ide.org/docs/tutorials/play/index.html#Setting_up_Play_2_1
 
適宜読み替えながら進めてみます。
 
 
まず、前節で導入したScala IDE for Eclipseのbundleに、Play Framework本体は含まれません!(含まれると思っていました。。)Play Frameworkのサイトから別途ダウンロードして、導入します。
 

f:id:tanakakoichi9230:20150402020226p:image

導入実施時点で、Play Frameworkの最新は「2.3.8」でした。
 
Play Frameworkは「2.3」から大きく構成を変えたようです。最も目立つ変更点は、従来の「play」コマンドが「activator」コマンドに置き換えられたことです。他、変更点が下記に記されています。(※上記"Downloads"のページ中の「Read the announcement」のリンク先です。)
 
 
このアナウンスを見ると、「2.3」からJava 8とScala 2.11をサポートした、とされています。JavaScalaの最新を使うにはPlay Framework 2.3が必須ということになります。
 
ダウンロードしたファイルのファイル名は下記の通り。
 
"typesafe-activator-1.3.2-minimal.zip"
 
"/usr/local/activator-1.3.2-minimal"に.zipを展開しました。
 
-----(ここから)-----
$ cd /usr/local/activator-1.3.2-minimal
$ ls -al
total 2416
drwxrwxr-x@  5 kou   staff      170  2 27 11:59 .
drwxr-xr-x  20 root  wheel      680  3 17 23:54 ..
-rwxrw-r--@  1 kou   staff     9507  2 27 11:59 activator
-rw-rw-r--@  1 kou   staff  1213547  2 27 11:59 activator-launch-1.3.2.jar
-rwxrw-r--@  1 kou   staff     7342  2 27 11:59 activator.bat
-----(ここまで)-----
 
いずれのファイル名にも"play"の文字列が含まれておらず、かつあまりにも内容物があっさりしていて戸惑いましたが、結論的にはこれでOKなのでした。Play Frameworkは「2.3」からは、もはや「Play Framework」のコマンドラインツールは独立には存在せず、Typesafe社のパッケージ管理ツール「Activator」に統合されたのです。「Play Framework」のダウンロードといっても、まずはその「Activator」をダウンロードせよ、ということになっているのです。
 
「activator new」などのコマンドを実行すると、必要なパッケージが随時"~/.ivy2"ディレクトリ下にダウンロードされる仕組みになっています。「activator」コマンドの初回実行時は大量のライブラリパッケージのダウンロードが発生します。かなり時間がかかります。
 
最後に".bashrc"に下記を追記しました。
 
-----(ここから)-----
# Play Framework 2.3
export PATH=$PATH:/usr/local/activator-1.3.2-minimal
-----(ここまで)-----
 
".bashrc"には、以前のバージョンのPlay FrameworkとScalaが必要とした環境変数PLAY_HOMEとSCALA_HOMEの定義が残っていたので、念のため削除(コメントアウト)しておきました。
 
Playアプリケーションの生成
 
".bashrc"書き換え後、新しいターミナルを開き、Scala IDE for EclipseのWorkspaceにするディレクトリに移動して、「activator new」コマンドでPlayアプリケーションの生成を試みました。
 
-----(ここから)-----
$ pwd
/Users/kou/Projects/ScalaIdeTest
$ ls -al
total 0
drwxr-xr-x   3 kou  staff   102  3 18 01:50 .
drwxr-xr-x@ 36 kou  staff  1224  3 17 00:46 ..
drwxr-xr-x   6 kou  staff   204  3 17 00:47 .metadata
$ activator new MyFirstPlayApp
Fetching the latest list of templates...
Browse the list of templates: http://typesafe.com/activator/templates
Choose from these featured templates or enter a template name:
  1) minimal-akka-java-seed
  2) minimal-akka-scala-seed
  3) minimal-java
  4) minimal-scala
  5) play-java
  6) play-scala
(hit tab to see a list of all templates)
>
Display all 262 possibilities? (y or n)
PlayStartApp                                          
activator-akka-cassandra                              
activator-akka-scala-guice                            
activator-akka-scala-parfait                          
activator-akka-spray                                  
activator-akka-tracing                                
activator-gilt-app                                    
activator-offline-test                                
activator-play-autosource-reactivemongo               
activator-play-tracing                                
・・・(略)・・・
test-patterns-scala                                   
tweetmap-java8                                        
tweetmap-workshop                                     
typeclass-tips                                        
unfiltered-basic-project                              
vaadin-in-akka                                        
webjars-sample-play2                                  
> play-scala
OK, application "MyFirstPlayApp" is being created using the "play-scala" template.
To run "MyFirstPlayApp" from the command line, "cd MyFirstPlayApp" then:
/Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp/activator run
To run the test for "MyFirstPlayApp" from the command line, "cd MyFirstPlayApp" then:
/Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp/activator test
To run the Activator UI for "MyFirstPlayApp" from the command line, "cd MyFirstPlayApp" then:
/Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp/activator ui
$ ls -al
total 0
drwxr-xr-x   4 kou  staff   136  3 18 01:53 .
drwxr-xr-x@ 36 kou  staff  1224  3 17 00:46 ..
drwxr-xr-x   6 kou  staff   204  3 17 00:47 .metadata
drwxr-xr-x  14 kou  staff   476  3 18 01:53 MyFirstPlayApp
$ cd MyFirstPlayApp/
$ ls -al
total 2448
drwxr-xr-x  14 kou  staff      476  3 18 01:53 .
drwxr-xr-x   4 kou  staff      136  3 18 01:53 ..
-rw-r--r--   1 kou  staff      141  3 18 01:53 .gitignore
-rw-r--r--   1 kou  staff      591  3 18 01:53 LICENSE
-rw-r--r--   1 kou  staff      148  3 18 01:53 README
-rwxr--r--   1 kou  staff     9507  3 18 01:53 activator
-rw-r--r--   1 kou  staff  1213547  3 18 01:53 activator-launch-1.3.2.jar
-rwxr--r--   1 kou  staff     7342  3 18 01:53 activator.bat
drwxr-xr-x   4 kou  staff      136  3 18 01:53 app
-rw-r--r--   1 kou  staff      210  3 18 01:53 build.sbt
drwxr-xr-x   4 kou  staff      136  3 18 01:53 conf
drwxr-xr-x   4 kou  staff      136  3 18 01:53 project
drwxr-xr-x   5 kou  staff      170  3 18 01:53 public
drwxr-xr-x   4 kou  staff      136  3 18 01:53 test
-----(ここまで)-----
 
途中で「template」を選ぶように言われています。「Activator」で生成できるアプリケーションはPlayアプリに限られないので「template」を指定しなけばなりません。(※「 template」はMavenのarchtypeに相当すると言えます。)tabキー押下でtemplateの全リストを出力するというので、tabキー押下しています。ちなみにその全リストをここに上げています。結局「play-scala」を選択(入力)しています。
 
上記のターミナルの出力の記録には出ていませんが、「Activator」の初回実行時には下記のようなライブラリパッケージのダウンロードが大量に発生します。かなり時間がかかります。
 
-----(ここから)-----
     [SUCCESSFUL ] com.typesafe.activator#activator-launcher;1.3.2!activator-launcher.jar (1863ms)
     [SUCCESSFUL ] org.scala-lang#scala-library;2.11.5!scala-library.jar (21601ms)
・・・
-----(ここまで)-----
 
アプリ生成後、「activator run」コマンドで、起動してみます。
 
-----(ここから)-----
$ pwd
/Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp
$ activator run
Getting org.scala-sbt sbt 0.13.5 ...
     [SUCCESSFUL ] org.scala-sbt#sbt;0.13.5!sbt.jar (1267ms)
・・・(略)・・・
[info] Done updating.
 
--- (Running the application, 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...)
 
 
[info] Compiling 5 Scala sources and 1 Java source to /Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp/target/scala-2.11/classes...
[info] 'compiler-interface' not yet compiled for Scala 2.11.1. Compiling...
[info]   Compilation completed in 21.844 s
[info] play - Application started (Dev)
 
-----(ここまで)-----
 
ここでターミナルの出力は停止するので、ブラウザで http://localhost:9000/ を開いてみます。次のような画面が出力されれば、Playアプリケーションの生成と実行は成功です。

f:id:tanakakoichi9230:20150402020206p:image

Playアプリケーションのプロセスの停止は「Ctrl-D」押下です。(※Ctrl-Cではない)
 
PlayアプリケーションのScala IDEへの取り込み
 
「activator eclipse」コマンドで、eclipse用のプロジェクトファイル(※".project"や".classpath"ファイル)を生成します。Playアプリケーション起動中の場合は「Ctrl-D」押下で停止してから「activator eclipse」コマンドを実行してください。
 
※かつては「play eclipsify」コマンドだったように記憶しますが、「activator」コマンドでは「activator eclipse」です。
 
-----(ここから)-----
pwd
/Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp
ls -al
total 2448
drwxr-xr-x  14 kou  staff      476  3 18 01:53 .
drwxr-xr-x   4 kou  staff      136  3 18 01:53 ..
-rw-r--r--   1 kou  staff      141  3 18 01:53 .gitignore
-rw-r--r--   1 kou  staff      591  3 18 01:53 LICENSE
-rw-r--r--   1 kou  staff      148  3 18 01:53 README
-rwxr--r--   1 kou  staff     9507  3 18 01:53 activator
-rw-r--r--   1 kou  staff  1213547  3 18 01:53 activator-launch-1.3.2.jar
-rwxr--r--   1 kou  staff     7342  3 18 01:53 activator.bat
drwxr-xr-x   4 kou  staff      136  3 18 01:53 app
-rw-r--r--   1 kou  staff      210  3 18 01:53 build.sbt
drwxr-xr-x   4 kou  staff      136  3 18 01:53 conf
drwxr-xr-x   4 kou  staff      136  3 18 01:53 project
drwxr-xr-x   5 kou  staff      170  3 18 01:53 public
drwxr-xr-x   4 kou  staff      136  3 18 01:53 test
activator eclipse
[info] Loading project definition from /Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp/project
[info] Set current project to MyFirstPlayApp (in build file:/Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp/)
[info] About to create Eclipse project files for your project(s).
[info] Successfully created Eclipse project files for project(s):
[info] MyFirstPlayApp
$ ls -al
total 2488
drwxr-xr-x  19 kou  staff      646  3 29 23:12 .
drwxr-xr-x   4 kou  staff      136  3 18 01:53 ..
-rw-r--r--   1 kou  staff    13225  3 29 23:12 .classpath
-rw-r--r--   1 kou  staff      141  3 18 01:53 .gitignore
-rw-r--r--   1 kou  staff      368  3 29 23:12 .project
drwxr-xr-x   3 kou  staff      102  3 29 23:12 .settings
-rw-r--r--   1 kou  staff      591  3 18 01:53 LICENSE
-rw-r--r--   1 kou  staff      148  3 18 01:53 README
-rwxr--r--   1 kou  staff     9507  3 18 01:53 activator
-rw-r--r--   1 kou  staff  1213547  3 18 01:53 activator-launch-1.3.2.jar
-rwxr--r--   1 kou  staff     7342  3 18 01:53 activator.bat
drwxr-xr-x   5 kou  staff      170  3 18 02:23 app
-rw-r--r--   1 kou  staff      210  3 18 01:53 build.sbt
drwxr-xr-x   4 kou  staff      136  3 18 01:53 conf
drwxr-xr-x   3 kou  staff      102  3 18 02:29 logs
drwxr-xr-x   6 kou  staff      204  3 18 02:10 project
drwxr-xr-x   5 kou  staff      170  3 18 01:53 public
drwxr-xr-x   8 kou  staff      272  3 29 23:10 target
drwxr-xr-x   5 kou  staff      170  3 29 23:12 test
 
-----(ここまで)-----
 
次に、ようやくScala IDEにPlayアプリケーションを取り込みます。Scala IDEの「File - Import...」メニューを選んで、ダイアログより「Existing Projects into Workspace」を選びます。

f:id:tanakakoichi9230:20150402020158p:image

「Import Projects」で先ほど生成したPlayアプリケーションのパスを設定します。(※この辺の操作は、Eclipseの普通の操作と同じです。)

f:id:tanakakoichi9230:20150402020152p:image

「Finish」押下で、Eclipseユーザーには見慣れた「Package Explorer」ペインが表示されます。
 
■ Scala IDEへの取り込みの続き 〜 ビルドエラーの除去
 
上記手順でScala IDEにProjectを取り込んだら、おそらく下記のようなビルドエラーが出ます。

f:id:tanakakoichi9230:20150402020236p:image

調べますと、下記記事が見つかりました。

 
Stack Overflow、"“object index is not a member of package views.html” when opening scala play project in scala ide":
 
結論的には、以下の追加作業が必要です。
 
1. Projectの「Properties」ダイアログを開いて、「Java Build Path」のページを開きます。

 

2. 「Add Folder...」ボタンを押下して、"target/scala-2.11/twirl/main"を選択、追加します。

f:id:tanakakoichi9230:20150402020308p:image

3. フォルダーが追加されたのを確認したら、「OK」を押下します。

f:id:tanakakoichi9230:20150402020248p:image

4. 自動Re-buildが走って、ビルドエラーが消えるはずです。

f:id:tanakakoichi9230:20150402020257p:image

ちなみに、「Twirl」とは、Play Frameworkに同梱されているテンプレートエンジンです。
 
"The template engine":https://www.playframework.com/documentation/2.3.x/ScalaTemplates
 
動作確認
 
ターミナルより、Playアプリケーションを「activator run」コマンドで起動しておきます。ブラウザで http://localhost:9000/ を開きます。
 
Scala IDEソースコードを編集して、

f:id:tanakakoichi9230:20150402020314p:image

保存します。

f:id:tanakakoichi9230:20150402020333p:image

ブラウザをリロードすると、変更が反映します。

f:id:tanakakoichi9230:20150402020324p:image

ブラウザをリロードすると、Playアプリケーションを起動したターミナルには下記表示が出ます。
 
-----(ここから)-----
$ activator run
[info] Loading project definition from /Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp/project
[info] Set current project to MyFirstPlayApp (in build file:/Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp/)
 
--- (Running the application, 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...)
 
[info] play - Application started (Dev)
[info] Compiling 1 Scala source to /Users/kou/Projects/ScalaIdeTest/MyFirstPlayApp/target/scala-2.11/classes...
 
--- (RELOAD) ---
 
[info] play - Application started (Dev)
 
-----(ここまで)-----
 
下記Tutorialには、auto-reloadを有効にするには、Playアプリケーションを「run」コマンドではなく「~ run」コマンドで起動せよと書いてありますが、
Play Framework 2.3.8では、単に「run」コマンドで、auto-reloadが有効な状態で起動するようです。
 
◆以上