CentOS7にTypesafeActivatorを使ってScalaとPlay framework2を導入してみた


最近？はやりのReactive Programmingの環境として、PlayFramework2をインストールしてみました。vagrantのcentos7上に構築しています。

```ruby:なんのことはないタダのVagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "centos7"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder "./data", "/vagrant", create: true
end
```

## typesafeActivatorをダウンロードする

現時点でのTypesafeActivatorは1.3.7が最新版でした。

```bash:console
$ wget http://downloads.typesafe.com/typesafe-activator/1.3.7/typesafe-activator-1.3.7.zip
```

## Javaをインストールする

JDK6+となっていますので、jdk6以上をインストールしましょう。

```bash:console
$ sudo yum install -y java-1.8.0-openjdk
$ sudo yum install -y java-1.8.0-openjdk-devel
```

## インストールする

```bash:console
$ ./activator-dist-1.3.7/activator
Unable to open the docs in your web browser.  To open them manually navigate to:
file:/home/vagrant/activator/README.html
 Did not detect an activator project in this directory.
 - activator
 Load an existing project (has to be executed from the project directory)
 or print this help message if no project is found

 Sub-commands
 - activator ui
 Open the project in the UI if executed from an existing project
 directory, otherwise open a project-creation UI.

 - activator new [project-name] [template-name]
 Create a new project, prompting for project-name if missing and helping you
 find a template if template-name is not provided.

 - activator list-templates
 Fetch the latest template list and print it to the console.
```
おっ動いてますね。CLIでなく、GUIですとactivator uiでかっこいいWebUIが表示されます。ちょーべんり。
![web ui](https://dommkopfq6m1m.cloudfront.net/assets/images/getstarted/77f32e3287f6330fa05cbe5e8596a784-all-create.png)

## PATHを通す

```bash:console
$ mv activator-dist-1.3.7/ activator
$ echo 'export PATH=$HOME/activator:$PATH' >> .bash_profile
$ source .bash_profile
$ activator
Unable to open the docs in your web browser.  To open them manually navigate to:
file:/home/vagrant/activator/README.html
 Did not detect an activator project in this directory.
 - activator
 Load an existing project (has to be executed from the project directory)
 or print this help message if no project is found

 Sub-commands
 - activator ui
 Open the project in the UI if executed from an existing project
 directory, otherwise open a project-creation UI.

 - activator new [project-name] [template-name]
 Create a new project, prompting for project-name if missing and helping you
 find a template if template-name is not provided.

 - activator list-templates
 Fetch the latest template list and print it to the console.
```
通った。:+1: 

## PlayFrameworkをインストールする

早速Playをインストールしたい。ｳｽﾞｳｽﾞ
`list-templates`でテンプレートを選びます。
play frameworkのテンプレートを探します。

```bash:console
$ activator list-templates | grep play | grep scala
  play-scala
  just-play-scala
  play-framework-scala-seed
  play-scala-2.3
  play-scala-preview
  play-scala-reactive-platform-15v09
  play-scala-test
  reactive-play-scala-akka-slick-guice-domain_validation-seed
  slim-play-scala
  hello-play-2_3-scala
  hello-play-scala
  play-oauth2-scala
  play-scala-angularjs-gulp
  play-scala-backbone-todo
  play-scala-intro
  play-scala-intro-reactive-platform-15v09
  play-scala-scalajs
  play-scalajs-showcase
  play-scalatest-subcut
  scalajs-play-core
  scalajs-play-core-react
```
結構たくさん出てきた。とりあえず、play-scalaを使ってみる。

```bash:console
$ activator new play play-scala
Fetching the latest list of templates...

OK, application "play" is being created using the "play-scala" template.

To run "play" from the command line, "cd play" then:
/home/vagrant/play/activator run

To run the test for "play" from the command line, "cd play" then:
/home/vagrant/play/activator test

To run the Activator UI for "play" from the command line, "cd play" then:
/home/vagrant/play/activator ui
```

```bash:console
$ cd play
$ ls
LICENSE    activator-launch-1.3.7.jar  build.sbt  project  test
README     activator.bat               conf       public
activator  app                         logs       target
```
playディレクトリにactivatorが入ってるので、さっきパスを通したactivatorではなく、こちらのアプリケーションごとに作成されるactivatorを使用します。

```bash:console
$ ./activator
Getting org.scala-sbt sbt 0.13.8 ...
downloading file:/home/vagrant/activator/repository/org.scala-sbt/sbt/0.13.8/jars/sbt.jar ...
        [SUCCESSFUL ] org.scala-sbt#sbt;0.13.8!sbt.jar (21ms)
downloading file:/home/vagrant/activator/repository/org.scala-lang/scala-library/2.10.4/jars/scala-$
ibrary.jar ...
        [SUCCESSFUL ] org.scala-lang#scala-library;2.10.4!scala-library.jar (147ms)
downloading file:/home/vagrant/activator/repository/org.scala-sbt/main/0.13.8/jars/main.jar ...
        [SUCCESSFUL ] org.scala-sbt#main;0.13.8!main.jar (88ms)
downloading file:/home/vagrant/activator/repository/org.scala-sbt/compiler-interface/0.13.8/jars/com
piler-interface-bin.jar ...
        [SUCCESSFUL ] org.scala-sbt#compiler-interface;0.13.8!compiler-interface-bin.jar (63ms)
downloading file:/home/vagrant/activator/repository/org.scala-sbt/compiler-interface/0.13.8/jars/com
piler-interface-src.jar ...
        [SUCCESSFUL ] org.scala-sbt#compiler-interface;0.13.8!compiler-interface-src.jar (8ms)
.
.
.
[info]  [SUCCESSFUL ] com.typesafe.play#build-link;2.4.6!build-link.jar (1055ms)
[info] downloading https://repo1.maven.org/maven2/com/typesafe/play/play-exceptions/2.4.6/play-excep
tions-2.4.6.jar ...
[info]  [SUCCESSFUL ] com.typesafe.play#play-exceptions;2.4.6!play-exceptions.jar (533ms)
[info] Done updating.
[info] Set current project to play (in build file:/home/vagrant/play/)
[play] $
```
かなり時間がかかった。activatorコンソールが起動されました。

```bash:console
[play] $ run

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

[info] p.c.s.NettyServer - 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...)

```
runでサーバを起動できます。このときにNode.jsをインストールしておくとassetsのコンパイルが早くなるらしい。また、consoleと入力すると、scalaのREPLが立ち上がります。

http://192.168.33.10:9000 にアクセスして、こんな感じの画面が出てくれば、一旦Playのインストールは完了です。

![Imgur](http://i.imgur.com/5Nae9aI.png)

拍子抜けするほど簡単でした。なんかもうちょっと、アレをインストールして...そしたらこれの依存関係があって...みたいなものだと思ってました。Scalaの勉強を時間見つけてやってみたいですね。