LoginSignup
5
5

More than 5 years have passed since last update.

ScalaでPlayFramework: 環境構築編

Last updated at Posted at 2016-12-10

触ってみたメモ。Macでの話です。
Playのバージョンは2.5.10です。

Scalaインストール

brewで入れます。簡単。

$ brew install scala
$ scala -version
Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL

sbtのインストール

sbtはビルドとかパッケージ管理をやってくれるものみたいです。
こちらもbrewで。

$ brew install sbt
...
[info] This is sbt 0.13.11
...

ここまでで、Hello Worldとかは書けるようになります。

PlayFrameworkのインストール

これもbrewで。playでいいのかなーと思ってたら、typesafe-activatorらしい。

$ brew install typesafe-activator

プロジェクトを作る

activator new [プロジェクト名]でプロジェクトを作ります。
今回はプロジェクト名をscala-play-sampleとしました。
コマンドを叩くと、どのテンプレートでプロジェクトを作るか聞かれるので、play-scalaを選びます。

$ activator new scala-play-sample

Fetching the latest list of templates...

Browse the list of templates: http://lightbend.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)
> 6
OK, application "scala-play-sample" is being created using the "play-scala" template.
...

コマンドが完了すると、scala-play-sampleというディレクトリが作られます。

起動してみる

「プロジェクトを作る」で生成されたディレクトリに移動して、activator runで起動できます。

$ activator 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...)

初回起動は依存パッケージのインストールをするみたいで、結構時間がかかります。信じて待ちましょう。
サーバが開始しました的なメッセージがでたら、ブラウザからアクセスしてみます。
http://localhost:9000/
Welcome to Playというページが表示されればOKです。
このページにどう開発すればいいか簡単に書いてあるので、目を通してもいいかもしれません。

デフォルトでは9000番ポートを使うことになっていますが、他のアプリケーションで既に使用している場合はポートを変える必要があります。
そんなときは以下の感じで起動します。ダブルクオートはちゃんとこのままつけてください。

$ activator "run 9999"
...
--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9999

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

起動方法に関しては色々あるみたいなので、そのうちまとめたい。
How do I change the default port (9000) that Play uses when I execute the “run” command?

Eclipseプラグインインストール

ここまでで開発できるようになってはいますが、楽したいのでEclipseでコード書けるようにします。
Scala IDE for Eclipseとやらがあるので、これ使えばいいじゃんと思ったのですが、自分の環境では起動時にエラーが出て動いてくれませんでした。

仕方ないので、既存のEclipseにプラグインを追加します。

http://scala-ide.org/download/current.html
上記から、Update site installationのURLをコピーします。
EclipseのHelp->Install New Softwareで、先程のURLを使って出て来るやつを全部インストールします。

これでEclipseでScalaが書けるようになりました。

activatorプロジェクトをEclipseプロジェクトに変換

「プロジェクトを作る」で作ったプロジェクトはそのままではEclipseで開くことはできないので、変換してやる必要があります。
まず、プロジェクトディレクトリ以下のproject/plugins.sbtを開き、以下を追加します。

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "バージョン")

バージョンは以下のページで確認できます。
typesafehub/sbteclipse

編集が終わったら以下のコマンドをプロジェクトルートで実行します。ダブルクオートはちゃんとこのままつけてください。

$ activator "eclipse with-source=true"
...
[info] Successfully created Eclipse project files for project(s):
[info] scala-play-sample

コマンドがエラー無く完了できたら、Eclipseでimportできるようになっています。

おわりに

ここまでで、Eclipseで開発するための環境構築が完了しました。
次の記事ではルーティングとコントローラをいじってみます。

次: ScalaでPlayFramework: コントローラ編

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5