Introduction
In this article, we will see the introduction to the sbt console, single commands, interactive mode, development mode, compilation, testing options, the launch of the scala console, debugging, sbt features, and play commands.
Sbt allows you to oversee a Play application's whole development process. Sbt allows you to enter commands simultaneously or in interactive mode (shell).
The interactive mode may become quicker as time passes because sbt only needs to launch once. But restarts each time you run it when you enter commands one at a time.
Single Commands in play console
You can immediately execute a single sbt command. For instance, to build and launch Play, navigate to the project's directory and execute:
$sbt run
You will see some content like this on command prompt
[info] Loading the project definition from /Users/play-developer/app/project
[info] Set current project to app (in build file:/Users/play-developer/app/)
--- (now, Running the application from sbt, auto-reloading is enabled) ---
[info] play - Listening for HTTP on address /0:0:0:0:0:0:0:0:8080
(Server started, please use Enter to stop and go back to the console...)
The application will starts directly. To return to the command prompt press Ctrl +D or enter
Interactive mode in play console
Change to the top-level of your project and type sbt without any parameters to start SBT in an interactive mode:
$ cd my-first-app
app $ sbt
You will see some content like this on command prompt
[info] Loading some global plugins from /Users/play-developer/.sbt/0.13/plugins
[info] Loading the project definition from /Users/play-developer/app/project
[info] Updating {file:/Users/play-developer/my-first-app/project/}app-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to my-first-app (in build file:/Users/play-developer/app/)
[app] $
Development Mode in play console
The auto-reload feature is turned on when Play is launched in this mode by sbt. If any files have changed since your last request, Play will recompile and restart your server immediately. The programme will restart automatically if necessary.
Run the current application in development mode with sbt in interactive mode by using the run command:
[app] $run
You will see some content like this on command prompt
$ sbt
[info] Loading the global plugins from /Users/play-developer/.sbt/1.0/plugins
[info] Loading the project definition from /Users/play-developer/tmp/app/project
[info] Done updating.
[info] Loading the settings for project root from build.sbt ...
[info] Set the current project to app (in build file:/Users/play-developer/tmp/app/)
[info] sbt server has started at local:///Users/play-developer/.sbt/1.0/server/c9c53f40a402da68f71a/sock
[app] $ run
[info] started Updating...
[info] updation done.
[warn] There may incompatibilities among your library dependencies; run 'evicted' to see the detailed eviction warnings.
--- (Running out the application, auto-reloading enabled) ---
[info] p.c.s.AkkaHttpServer - started Listening for HTTP on /0:0:0:0:0:0:0:0:8080
(Server started, use Enter to stop and go back to the console...)
Compilation in play console
Your application can also be built without the HTTP server active. Any application errors are shown in the command window by the build command. Enter this as an example in the interactive mode:
[app] $compile
You will see some content like this on command prompt
[app] $ compile
[info] Compiling the 1 Scala source to /Users/play-developer/app/target/scala-2.13/classes...
[error] /Users/play-developer/app/app/controllers/HomeController.scala:21: not found: value Actionx
[error] def index = Actionx { implicit request =>
[error]
[error] one error is found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 1 s, completed AUG 17, 2022 2:09:01 PM
[app] $
When all the errors are rectified then this content will be shown
[app] $ compile
[info] Updating {file:/Users/play-developer/app/}root...
[info] Resolving jline#jline;2.12.2 ...
[info] Done updating.
[info] Compiling the 8 Scala sources and 1 Java source to /Users/play-developer/app/target/scala-2.13/classes...
[success] Total time: 3 s, completed AUG 17, 2022 2:10:00 PM
[app] $
Testing options in play console
Tests can be run without the server being active. For illustration, use the test command in interactive mode:
[app] $ test
All of your project's tests will be executed using the test commands. Additionally, you can choose certain tests by using testOnly:
[app] $ testOnly com.acme.SomeClassesTest
Launch the Scala in play console
You may test your code interactively by typing console to open the Scala console:
[app] $ console
To launch an application inside the Scala console (for instance, to access a database):
import play.api.
val a = Environment(new java.io.File("."), this.getClass.getClassLoader, Mode.Dev)
val context = ApplicationLoader.Context.create(a)
val loader = ApplicationLoader(context)
val app = loader.load(context)
Play.start(app)
Debugging in play console
You can instruct Play to launch a JPDA debug port when the console boots up. Afterward, you can connect using the Java debugger. To achieve it, issue the following command: sbt -jvm-debug port>
$sbt -jvm-debug-8000
During boot, the JVM will log this line if a JPDA port is accessible:
Listening for transport dt_socket at address: 8000
Using sbt feature
You can use the sbt feature triggering execution.
For example, using ~ compile:
[app] $ ~ compile
The compilation will triggered each time you change a source file.
If you use ~ run:
[app] $ ~ run
The triggered compilation will enable when a development server is running.
You can do the same for ~ test, to continuously check your project each time when you modify a source file:
[app] $ ~ test
This could become useful if you want to run a small set of your tests using testOnly command. For instance:
[app] $ ~ testOnly com.acme.SomeClassTest
This Will trigger the execution of com.acme.SomeClassTest test every time you modify a source file.
Using the play command directly
Direct command execution is another option if you don't want to use the Play console. For instance, type sbt run:
$ sbt run
[info] Loading the project definition from /Users/play-developer/app/project
[info] Set the current project to my-first-app (in build file:/Users/play-developer/app/)
--- (Running the application from sbt, auto-reloading is enabled) ---
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:8080
(Server started, use Enter to stop and go back to the console...)
The program launches immediately. Ctrl+D or Enter will return you to your OS prompt after you terminate the server.
The server's port number by default is 8080. sbt 'run 3000' can be used to specify a custom port, such as 3000.
The triggered execution is available here as well, of course:
$ sbt run
Frequently Asked Questions
How should I launch a play framework?
Running a Play application Make a fresh Run Configuration. Run -> Edit Configurations can be found on the main menu. In order to add a new configuration, click the +. Select "sbt Task" from the configurations list.
How do I launch a debug in the Play Framework?
Start your program with the activator -jvm-debug 9999 run to debug it, and then right-click the project in Eclipse and choose Debug As, Debug Configurations. Right-click on Remote Java Application in the Debug Configurations dialogue and choose New. Switch the port to 9999, then click Apply.
In play framework, what is sbt?
Your project's parameters are defined in the sbt file. According to the sbt manual, you can also define your own unique custom parameters for your project. Knowledge of the sbt settings, in particular, is helpful.