Crafting a Scala Template
I've been focusing on the user's need for a Scala Play boilerplate, essentially a kickstart. I'm now zeroing in on the core components needed: essential dependencies, project structure, and rudimentary controllers. I'm also considering how to best represent this boilerplate – a simple repository, or something more interactive?
Listing Essential Elements
I've identified the key files and directory structure necessary for a functional Play app using sbt, covering build definitions, routes, configuration, controllers, and views. Considering the "flavor," I'm opting for the current standard: Play 3.0 based on Pekko, using Guice for dependency injection, and of course, leveraging Scala as the language. I am beginning to draft the components.
Outlining Core Components
I'm now detailing the directory structure, presenting it visually to aid user comprehension. Key files like project/plugins.sbt and build.sbt are taking shape; I've drafted contents for them, including the Play plugin and standard sbt versions. Dependencies are a key consideration, I'm working now to incorporate necessary Scala libraries into the build.sbt file.
Developing Sample Code
I've been working on a basic controller with a corresponding route and view. I've sketched a minimal HomeController in Scala, illustrating how to set up dependency injection with Guice and return a simple "Welcome" message. The routes file is now equally simple, routing requests to that controller's index action. I also created an index.scala.html as the corresponding default view.
Creating a Basic View
I've sketched a simple Twirl template, the core part of our boilerplate, displaying a message. I'm exploring the best way to present it. Now I am thinking about user needs: providing a simple code block is acceptable, but highlighting the utility of an sbt generator is crucial. I am thinking about including clear instructions on running it with sbt run and what files contribute to the system.
Confirming Key Framework Elements
I'm now in the final stage, ensuring all core components align. I've rechecked dependency groups and confirmed using play.api.mvc for core controller functionality. I've meticulously reviewed the build.sbt to verify that PlayScala is enabled, and I'm currently cross-checking the routes file for proper syntax. With these checks in place, I feel confident moving toward generating the output.
Here are two ways to get a Scala Play Framework application up and running.
Method 1: The Quickest Way (Command Line)
The official and recommended way to generate a boilerplate is using sbt new. Run this in your terminal:
sbt new playframework/play-scala-seed.g8
Follow the prompts to name your project.
Method 2: The Manual Way (Copy-Paste)
If you prefer to understand every file being created, here is the essential code to build a Play 3.x (Pekko-based) application from scratch.
1. Project Structure
Create folders to match this tree:
my-play-app/
├── app/
│ ├── controllers/
│ │ └── HomeController.scala
│ └── views/
│ └── index.scala.html
├── conf/
│ ├── application.conf
│ └── routes
├── project/
│ ├── build.properties
│ └── plugins.sbt
└── build.sbt
2. Configuration Files
project/build.properties
Defines the sbt version.
sbt.version=1.9.9
project/plugins.sbt
Adds the Play framework plugin. Note the organization change to org.playframework for Play 3.
addSbtPlugin("org.playframework" % "sbt-plugin" % "3.0.2")
build.sbt
The main build definition.
name := "my-play-app"
organization := "com.example"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.13" // Or "3.3.3"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.0" % Test
conf/application.conf
Main configuration file (secret key, database, etc). You can leave it empty for a "Hello World", but it must exist.
# https://www.playframework.com/documentation/latest/Configuration
play.http.secret.key = "changeme"
conf/routes
Maps HTTP requests to Controller methods.
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
GET / controllers.HomeController.index
GET /assets/*file controllers.Assets.versioned(path="/public", file)
3. Application Logic
app/controllers/HomeController.scala
The logic handling the request.
package controllers
import javax.inject._
import play.api.mvc._
/**
* This controller creates an `Action` to handle HTTP requests to the
* application's home page.
*/
@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("Your new application is ready."))
}
}
app/views/index.scala.html
The view template (using Twirl syntax).
@(message: String)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome to Play</title>
</head>
<body>
<h1>Welcome to Play!</h1>
<p>@message</p>
</body>
</html>
How to Run
- Open your terminal in the root directory (
my-play-app).
- Run the development server:
sbt run
(Note: The first time you run this, it will take a few minutes to download dependencies).
- Open your browser to
http://localhost:9000.