Framing the Boilerplate
I'm now zeroing in on the essence of this Play boilerplate. It's about laying a solid foundation: the directory structure, crucial config files, and starter code. The aim is a jumpstart for anyone crafting a Play app in Scala. It's less about complete features, more about that initial setup phase to get people up and running, focusing on a robust base.
Refining the Foundation
I'm now fully immersed in the core components. I've got the sbt setup: build.properties, plugins.sbt, and build.sbt are squared away. Key config files like routes and application.conf are on the radar. The goal is a straightforward controller and Twirl template for that essential "Hello World" functionality. This is the minimum viable boilerplate.
Including Asset Management
I've added static assets to the mix - css, js, images. It's optional, sure, but a boilerplate should strive for completeness. I'm focusing on two approaches for content delivery: the "g8" template method, which is the official route, and a "manual setup" option for clarity. I've begun drafting the file structure and contents. I'm building out project/plugins.sbt with the Play plugin, and defining the build.sbt essentials. The plan now is to draft a barebones controller and view.
Constructing the Core
I'm now fully immersed in the core components. I've mapped GET / to HomeController.index in conf/routes, and the index.scala.html view is ready. I'm focusing on two output structures: the G8 template, and a clear, manual file layout. Ensuring a Play 3.0 recommendation is paramount, although I recognize legacy users might prefer Play 2.8. I'm drafting the response, including "fast" and "manual" approaches, instructions, and a quick tip on JSON handling with play-json.
Confirming the Boilerplate
I've finalized all the code snippets. The build.sbt, plugins.sbt, and controller are set. They represent a clear starting point for any Play Scala project. Now it's time to generate the output, ensuring a concise and useful boilerplate.
Here is a modern boilerplate for a Scala Play Framework (version 3.0+) application.
Play 3.0 is the current standard (which migrated from Akka to Apache Pekko). You can set this up in two ways: using the official generator or manually creating the files.
Option 1: The Quick Way (Recommended)
If you have sbt installed, the standard way to generate a boilerplate is via the terminal:
sbt new playframework/play-scala-seed.g8
Option 2: Manual Setup (Copy-Paste Boilerplate)
If you want to build the project structure yourself to understand the parts, here are the essential files.
1. Directory Structure
Create folders to match this hierarchy:
my-play-app/
├── app/
│ ├── controllers/
│ │ └── HomeController.scala
│ └── views/
│ └── index.scala.html
├── conf/
│ ├── application.conf
│ ├── logback.xml
│ └── routes
├── project/
│ ├── build.properties
│ └── plugins.sbt
└── build.sbt
2. The Build Files
project/build.properties
Defines the sbt version.
sbt.version=1.9.9
project/plugins.sbt
Adds the Play framework sbt plugin.
addSbtPlugin("org.playframework" % "sbt-plugin" % "3.0.2")
build.sbt
Project dependencies and settings.
name := """my-play-app"""
organization := "com.example"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
// Using Scala 3 (you can also use 2.13.12)
scalaVersion := "3.3.3"
libraryDependencies ++= Seq(
guice, // Dependency Injection
"org.scalatestplus.play" %% "scalatestplus-play" % "7.0.1" % Test
)
3. The Configuration
conf/routes
Defines how URLs map to code.
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET / controllers.HomeController.index()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file)
conf/application.conf
For now, this can remain empty, but this is where database and secret configs go.
# https://www.playframework.com/documentation/latest/Configuration
play.http.secret.key = "changeme"
conf/logback.xml
Basic logging configuration.
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
4. The Application Code
app/controllers/HomeController.scala
The entry point for your request.
package controllers
import javax.inject._
import play.api._
import play.api.mvc._
@Singleton
class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
/**
* Create an Action to render an HTML page.
* The configuration in the `routes` file means that this method
* will be called when the application receives a `GET` request with
* a path of `/`.
*/
def index() = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index())
}
}
app/views/index.scala.html
The template file (Twirl template engine).
@()
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome to Play</title>
</head>
<body>
<h1>It works!</h1>
<p>Welcome to your Scala Play application.</p>
</body>
</html>
How to Run
- Open your terminal in the root folder (
my-play-app).
- Run sbt:
sbt run
- Wait for dependencies to download. You will eventually see:
--- (Running the application, auto-reloading is enabled) ---
- Open your browser to
http://localhost:9000.
Bonus: Adding JSON Support
If you are building an API, you will likely need JSON support immediately.
- Add this to
build.sbt in libraryDependencies:
"org.playframework" %% "play-json" % "3.0.2"
- Update your controller to return JSON:
import play.api.libs.json.Json
def index() = Action { implicit request =>
Ok(Json.obj("message" -> "Hello API"))
}