📚

Play Framework × Scala 環境構築のメモ

に公開

こんな人向け

  • 初めてScalaを触る人
  • 初めてPlay Frameworkを触る人

この記事のゴール

  • Play Framework のチュートリアル用で用意されているサンプルを動かす
  • サンプルのコードが覗けるようになる

記事の概要

  • Play Framework (と Scala) での、環境構築についての簡単なメモです。
  • チュートリアルですでに作成されたサンプルコードを、覗けるようになるトコまでを記載してます。

環境構築

前提条件

  • Windows11
  • Visual Studio Code

Java Development Kit (JDK) のインストール

(下手に説明するより以下を見てもらったほうが確実っす…)

sbt (Scala Build Tool) のインストール

msi をダブルクリックして起動 & Nextを押下

ライセンス同意にチェックを入れる & Nextを押下

Next を押下

Install を押下

インストール完了

ちゃんとインストールされたか確認

PS C:\Users\xxxx> sbt --script-version
1.10.0

Play Framework サンプル & チュートリアルのコードを取得

チュートリアル用プロジェクトの実行

  • 先ほどクローンしてきたサンプルプロジェクトの内、今回は 「play-scala-hello-world-tutorial」をお試しで動かしてみます。
  • クローンしたフォルダで powershell を起動します。
  • 以下、play-scala-hello-world-tutorial ディレクトリへ移動
PS C:\Users\xxxx\play-samples> cd .\play-scala-hello-world-tutorial\
  • 以下、コマンドを実行して起動します。
PS C:\Users\xxxx\play-samples\play-scala-hello-world-tutorial> sbt run
  • 以下のようにログが出力されて (Server started, ...) が表示されてればOK。
[info] welcome to sbt 1.10.11 (Eclipse Adoptium Java 17.0.15)
[info] loading settings for project play-scala-hello-world-tutorial-build from plugins.sbt...
[info] loading project definition from D:\project\play-samples\play-scala-hello-world-tutorial\project
[info] loading settings for project root from build.sbt...
[info]   __              __
[info]   \ \     ____   / /____ _ __  __
[info]    \ \   / __ \ / // __ `// / / /
[info]    / /  / /_/ // // /_/ // /_/ /
[info]   /_/  / .___//_/ \__,_/ \__, /
[info]       /_/               /____/
[info]
[info] Version 3.0.8 running Java 17.0.15
[info]
[info] Play is run entirely by the community. Please consider contributing and/or donating:
[info] https://www.playframework.com/sponsors
[info]

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

INFO  p.c.s.PekkoHttpServer - Listening for HTTP on /[0:0:0:0:0:0:0:0]:9000

(Server started, use Enter to stop and go back to the console...)
  • お好みのブラウザで、以下のURLを入力し、ちゃんと起動出来ているかを見てみます。
http://localhost:9000/
  • 以下の 「Play Hello World Web Tutorial」の画面が表示されてればOK!

Visual Studio Code 拡張機能のインストール

  • Play Frameworkのライブラリの中身とかを見るときに大変便利なので、VSCode の拡張機能を入れちゃいます。
  • Scala (Metals) を入れます。
  • 拡張機能が無事にインストールできたら、お試しで 先ほどの「play-scala-hello-world-tutorial」 HomeController クラス の継承元 「AbstractController」 を覗いてみます。

ctrl + マウスオーバー で ポップアップを表示

そのままクリックすると Controller.scala の AbstractController へジャンプ

  • もしジャンプできない場合、拡張機能が上手く有効化されてないかも…? (Visual Studio Code の再起動 またはエラーの通知が出てないかを確認。)

さいごに

  • 本記事では、Play Frameworkの環境構築~サンプルコードを動かす&覗く トコまでを記載しました。
  • Playframework の日本語があるドキュメントのバージョンと、 最新版のドキュメントで内容が結構違いますので、
    Chat GPT とかで相談したり質問する場合は、必ず参照元を見ておいた方が良いと思います。(何回か古いバージョンの資料を元に回答されたので…。)

Discussion