The user wants a "boilerplate" for a Scala Play Framework application.
What does "boilerplate" mean in this context?
It usually means a minimal, working starting point that includes the essential file structure, build configuration, a simple controller, a simple view, and maybe a basic route. It should be ready to run with minimal effort.
What are the key components of a Play application?
- Build Tool: Usually sbt (Scala Build Tool). Sometimes Gradle or Maven, but sbt is the standard. I should provide the
build.sbt and project/build.properties.
- Project Structure:
app/ (source code)
conf/ (configuration)
public/ (assets)
test/ (tests)
- Code:
- A Controller (e.g.,
HomeController).
- A Twirl template (e.g.,
index.scala.html).
- Running Instructions: How to start the server.
Which version of Play?
Play 2.8.x is very stable. Play 2.9.x is the current modern generation. I should probably target Play 2.9 (using sbt 1.x and Scala 2.13 or 3.x). Let's stick to Scala 2.13 for maximum compatibility with existing libraries, though Scala 3 support in Play is good now. I'll use Scala 2.13 and Play 2.9.x as the safe, modern default.
Step-by-Step Plan:
- Directory Layout: Briefly visualize the tree.
build.sbt: Define name, version, library dependencies (filters, guice/hikari - usually default). Since Play 2.9, adding specific dependencies might be slightly different, but standard sbt new templates are the best reference.
project/build.properties: Specify sbt version.
project/plugins.sbt: Add the Play sbt plugin (essential for running the app and compilation).
conf/routes: Define the root route.
conf/application.conf: Basic config (server port, etc.).
app/controllers/HomeController.scala: The logic.
app/views/index.scala.html: The UI.
- How to Run: Command line instructions.
Refining the Content:
- Dependency Injection: Play uses Guice by default in the standard templates, but recently there's been a shift towards compile-time DI. However, for a "boilerplate" request, the user likely expects the standard runtime DI (Guice) approach because it's easier to set up initially. I will stick to the standard
AbstractController.
- Twirl: Need to import
play.twirl.api.Html.
Self-Correction/Check:
Should I just tell them to run sbt new playframework/play-scala-seed.g8?
- Pros: It's the official way, ensures the latest versions.
- Cons: The user asked for "boilerplate", implying they might want to see the file structure and code to understand it or copy-paste it manually into an existing empty project.
- Decision: I will provide the full code breakdown so they can build it from scratch, but also mention the
sbt new command at the beginning or end as the "easy button".
Drafting the code snippets:
build.sbt:
name := """play-scala-boilerplate"""
version := "1.0.0"
scalaVersion := "2.13.12"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.1.0" % Test
// Play 2.9 requires Java 11 or 17.
plugins.sbt:
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.9.0")
HomeController:
package controllers
import javax.inject._
import play.api.mvc._
@Singleton
class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index("Your new application is ready."))
}
}
views/index.scala.html:
@(message: String)
<!DOCTYPE html>
<html>
<body>
<h1>@message</h1>
</body>
</html>
Let's structure the output clearly with a directory tree and file contents.
Version Check:
Play 2.9.x is the latest stable series.
SBT 1.9.x is standard.
Scala 2.13 is the safest bet for a boilerplate.
I'll create a comprehensive response that includes:
- The recommended CLI command to generate it.
- The manual file-by-file setup for educational purposes or custom setup.
- A brief explanation of what each file does.
Here are two ways to get a Scala Play Framework boilerplate.
Option 1: The Standard Way (Recommended)
The Play Framework maintainers provide a official template generator. This is the best way to ensure you have the latest folder structure and build configurations.
Open your terminal and run:
sbt new playframework/play-scala-seed.g8
This will prompt you for a project name and version, then generate a working application.
Option 2: The Manual Way (From Scratch)
If you want to understand the file structure or create one manually, here is the minimal boilerplate code for a Play 3.0 (the latest version) application using Scala 3.
1. Directory Structure
Create a folder structure that looks like this:
my-project/
├── app/
│ ├── controllers/
│ │ └── HomeController.scala
│ └── views/
│ └── index.scala.html
├── conf/
│ ├── application.conf
│ └── routes
├── project/
│ └── build.properties
├── build.sbt
└── .gitignore
2. build.sbt
Define your dependencies. Note that Play 3 requires Java 17 or higher.
name := """my-play-app"""
version := "1.0.0"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "3.3.1"
libraryDependencies ++= Seq(
guice,
"org.scalatestplus.play" %% "scalatestplus-play" % "6.0.0" % Test
)
3. project/build.properties
Specify the SBT version.
sbt.version=1.9.7
4. project/plugins.sbt
Add the Play SBT plugin.
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "3.0.1")
5. conf/routes
Define your URL routing.
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
GET / controllers.HomeController.index
6. conf/application.conf
Basic configuration.
# This is the main configuration file for the application.
# https://www.playframework.com/documentation/latest/Configuration
play.http.secret.key = "changeme"
7. app/controllers/HomeController.scala
The controller logic.
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
@Singleton
class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index())
}
}
8. app/views/index.scala.html
The Twirl template file.
@()
<!DOCTYPE html>
<html lang="en">
<head>
<title>Play Scala Boilerplate</title>
</head>
<body>
<h1>Hello, Play Framework!</h1>
<p>Your application is ready.</p>
</body>
</html>
How to Run
- Open your terminal in the root directory.
- Run
sbt run.
- Open your browser to
http://localhost:9000.