Scala Play2 Introduction

【Scala / play2 導入メモ】2013/12/14~

Play2 version: 2.2.0 Scala version: 2.10.2 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).

以下、ホームディレクトリにて行う。

■準備

事前に Java 開発環境のインストールをしておくこと。

1
$ sudo aptitude install openjdk-6-jdk

■play2 フレームワークの取得と展開

1
2
3
4
5
6
7
8
9
$ wget http://downloads.typesafe.com/play/2.2.0/play-2.2.0.zip

$ unzip play-2.2.0.zip

$ sudo mv play-2.2.0 /usr/local

$ cd /usr/bin

$ sudo ln -s /usr/local/play-2.2.0/play play

■動作確認

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ play

zsh: correct 'play' to 'aplay' [nyae]? n
Getting com.typesafe.play console_2.10 2.2.0 ...
:: retrieving :: org.scala-sbt#boot-app
confs: [default]
6 artifacts copied, 0 already retrieved (2012kB/93ms)
Getting Scala 2.10.2 (for console)...
:: retrieving :: org.scala-sbt#boot-scala
confs: [default]
5 artifacts copied, 0 already retrieved (24390kB/2078ms)
_
 _ __ | | __ _ _  _
 | '_ \| |/ _' | || |
 |  __/|_|\____|\__ /
 |_|            |__/

play 2.2.0 built with Scala 2.10.2 (running Java 1.6.0_18), http://www.playframework.com

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

■Scalaプロジェクト作成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$ play new testPlay

zsh: correct 'play' to 'aplay' [nyae]? n
_
 _ __ | | __ _ _  _
 | '_ \| |/ _' | || |
 |  __/|_|\____|\__ /
 |_|            |__/

play 2.2.0 built with Scala 2.10.2 (running Java 1.6.0_18), http://www.playframework.com

The new application will be created in /home/philippos/testPlay

What is the application name? [testPlay]
>

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

1             - Create a simple Scala application
2             - Create a simple Java application

> 1
OK, application testPlay is created.

Have fun!

$ tree testPlay

testPlay
|-- README
|-- app
|   |-- controllers
|   |   `-- Application.scala
|   `-- views
|       |-- index.scala.html
|       `-- main.scala.html
|-- build.sbt
|-- conf
|   |-- application.conf
|   `-- routes
|-- project
|   |-- build.properties
|   `-- plugins.sbt
|-- public
|   |-- images
|   |   `-- favicon.png
|   |-- javascripts
|   |   `-- jquery-1.9.0.min.js
|   `-- stylesheets
|       `-- main.css
`-- test
    |-- ApplicationSpec.scala
    `-- IntegrationSpec.scala

10 directories, 14 files

■playアプリケーションの実行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ play run

[info] Loading project definition from /home/philippos/testPlay/project
[info] Set current project to testPlay (in build file:/home/philippos/testPlay/)


Cannot load the JNotify native library (/home/philippos/testPlay/target/native_libraries/64bits/libjnotify.so: /lib/libc.so.6: version `GLIBC_2.12' not found (required by /home/philippos/testPlay/target/native_libraries/64bits/libjnotify.so))
Play will check file changes for each request, so expect degraded reloading performace.

--- (Running the application from SBT, 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 /home/philippos/testPlay/target/scala-2.10/classes...
[info] play - Application started (Dev)

下記URLでアクセスし動作を確認する。

http://localhost:9000/

■対話型実況環境(REPL)の起動

1
2
3
4
5
6
7
8
$ play console                                                                                                                                                                    [21:19:14]
[info] Loading project definition from /home/philippos/testPlay/project
[info] Set current project to testPlay (in build file:/home/philippos/testPlay/)
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.2 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.

終了するときは、:quit とする。


■参考サイト:

http://www.playframework.com/download
http://yksris.hatenablog.com/entry/2013/01/06/134353
http://tech-sketch.jp/2013/11/scala-play2-1.html