LoginSignup
7
8

More than 5 years have passed since last update.

今さらながらPlay Framework を触ってみる

Last updated at Posted at 2016-04-02

概要

業務でPlay Framework を使って書かれたwebサービスを紐解く必要が出てきたため、Play Framework を理解するために環境構築の構築から新規アプリケーションを作るところまで試してみる。

環境構築

今回はMac OS ver 10.9.5 にて環境構築を行う。

JDK インストール

こちらを参考にインストールするだけ
MacにJava(JDK)をインストール

activator とPlay Framework のダウンロード

play はactivator によって提供されるようなので、まずactivator のダウンロードから行う。
※activator についてはこちらを参照

activator ダウンロード

公式ページへアクセスし、最新のパッケージをダウンロード
http://www.playframework.org/
PlayFramework_top.png

activator のPath 追加

ダウンロードが完了したらそれぞれのフォルダを任意の場所に設置。
activator 及びのPath を通す必要があるのでbash, zsh 等環境に合わせてPath を追加。
以下は$HOME/Library 配下にフォルダを移した場合に追加するPath
${HOME}/Library/activator-1.3.9-minimal/bin

activator のPath が通ったかの検証

$ activator -help

成功例

% activator -help
Usage: activator <command> [options]

  Command:
  ui                 Start the Activator UI
  new [name] [template-id]  Create a new project with [name] using template [template-id]
  list-templates     Print all available template names
  -h | -help         Print this message

  Options:
  -v | -verbose      Make this runner chattier
  -d | -debug        Set sbt log level to debug
  -mem <integer>     Set memory options (default: , which is -Xms1024m -Xmx1024m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m)
  -jvm-debug <port>  Turn on JVM debugging, open at the given port.

  # java version (default: java from PATH, currently java version "1.8.0_77")
  -java-home <path>  Alternate JAVA_HOME

  # jvm options and output control
  -Dkey=val          Pass -Dkey=val directly to the java runtime
  -J-X               Pass option -X directly to the java runtime
                     (-J is stripped)

  # environment variables (read from context)
  JAVA_OPTS          Environment variable, if unset uses ""
  SBT_OPTS           Environment variable, if unset uses ""
  ACTIVATOR_OPTS     Environment variable, if unset uses ""

In the case of duplicated or conflicting options, the order above
shows precedence: environment variables lowest, command line options highest.

play ダウンロード

同様に公式ページよりインストール
https://www.playframework.com/download
PlayDownloads.png

play のPath 追加

activator と同じく展開したフォルダを任意の場所に設置し、Path を通す
以下は$HOME/Library 配下にフォルダを移した場合に追加するPath

${HOME}/Library/play-2.2.6

play のPath が通ったかの検証

以下のコマンドを実行し、正常にヘルプが表示されればOK

$ play help

成功例

% play help
Getting com.typesafe.play console_2.10 2.2.6 ...
:: retrieving :: org.scala-sbt#boot-app
    confs: [default]
    6 artifacts copied, 0 already retrieved (2012kB/139ms)
Getting Scala 2.10.3 (for console)...
:: retrieving :: org.scala-sbt#boot-scala
    confs: [default]
    5 artifacts copied, 0 already retrieved (24447kB/319ms)
       _
 _ __ | | __ _ _  _
| '_ \| |/ _' | || |
|  __/|_|\____|\__ /
|_|            |__/

play 2.2.6 built with Scala 2.10.3 (running Java 1.8.0_77), http://www.playframework.com
Welcome to Play 2.2.6!

These commands are available:
-----------------------------
license            Display licensing informations.
new [directory]    Create a new Play application in the specified directory.

You can also browse the complete documentation at http://www.playframework.com.

新規プロジェクトの作成

以下のコマンドで新しいプロジェクトが作られる

$ play new [プロジェクト名]

実行例、今回はJava で作成。
(アプリケーション名を問われた際には何も入力せずEnterを打っています)

% play new playSampleProject
       _
 _ __ | | __ _ _  _
| '_ \| |/ _' | || |
|  __/|_|\____|\__ /
|_|            |__/

play 2.2.6 built with Scala 2.10.3 (running Java 1.8.0_77), http://www.playframework.com

The new application will be created in /Users/XXXX/work/playSampleProject

What is the application name? [playSampleProject]
>

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

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

> 2
OK, application playSampleProject is created.

Have fun!

できました^^

アプリの起動

コンソールの起動

先ほど作成したプロジェクトディレクトリに移動し、playコマンドを実行。
初回の起動では必要なファイルがダウンロードされます。

% play
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0
Getting org.scala-sbt sbt 0.13.0 ...
:: retrieving :: org.scala-sbt#boot-app
    confs: [default]
    43 artifacts copied, 0 already retrieved (12440kB/242ms)
[info] Loading project definition from /Users/XXXX/work/playSampleProject/project
[info] Set current project to playSampleProject (in build file:/Users/XXXX/work/playSampleProject/)
       _
 _ __ | | __ _ _  _
| '_ \| |/ _' | || |
|  __/|_|\____|\__ /
|_|            |__/

play 2.2.6 built with Scala 2.10.3 (running Java 1.8.0_77), http://www.playframework.com

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

[playSampleProject] $

なんか表示が変わった、コンソールが起動したぽい

アプリの起動

この状態でrun を実行

[playSampleProject] $ run
[info] Updating {file:/Users/XXXX/work/playSampleProject/}playsampleproject...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.

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

※デフォルトのポート番号は9000。既に利用している場合はrun [port番号]のように、使用していないポート番号を指定する。

起動したアプリにアクセス

http://localhost:9000 にアクセスし、以下のような画面が表示されることを確認。
image

次回

次は生成されたプロジェクトの構成を調査します。

参考サイトまとめ

Java開発で泣かないためのPlay frameworkの基礎知識
MacにJava(JDK)をインストール

7
8
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
7
8