Table of contents
1.
Introduction
2.
Connecting to Play's Development Mode
2.1.
Methods in PlayRunHook to Start the Play’s Development Mode
2.2.
Building Web Application using Grunt
3.
Adding Compiler Options in Development Mode
4.
Adding Additional Asset Directory to Build System
5.
Configuring Externalized Resources to Build System
6.
Disable Documentation to Speed up the Compilation
7.
Setting ivy logging level
8.
Fork and Parallel Execution in Test
9.
Frequently Asked Questions
9.1.
How does Play Framework's routing work?
9.2.
How can one generate and return views on a request/response cycle?
9.3.
What are Akka Actors in the play framework?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

Cookbook

Author Ayush Mishra
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this digital age, having a website for your business is a must. It enables your company to reach a larger audience and acquire more business leads. Unlike a few years ago, the extensive demand for web development has led to the inception of various frameworks such as the Play framework, etc.

Play is an efficient Java and Scala web application framework that integrates elements and APIs for modern web application development. It communicates with various layers of object-relational mapping (ORM) and has no prior assumptions about how to access databases. It provides a reactive programming model that makes use of the HTTP Event driven layer.

Cookbook in the Play application system.

In this blog, we will discuss Cookbook in detail to build the Play application system. Let’s start going!

Connecting to Play's Development Mode

At the very beginning of the Sbt Cookbook, we will see running a play in development mode using sbt run; it's useful to connect to this to initiate extra development procedures. This can be achieved by defining a PlayRunHook.

Boy did not know how to start the play development mode.

Methods in PlayRunHook to Start the Play’s Development Mode

In this part of the Sbt cookbook, we will see methods in PlayRunHook to start to play Development Mode:-

1beforeStarted(): Unit - This method is called before the start of the play application, but after all "before run" tasks have been accomplished.

2afterStarted(): Unit - This method is invoked after the play application has been launched.

3afterStopped(): Unit - This method is invoked after the play process has been terminated.

Building Web Application using Grunt

In this part of the Sbt cookbook, we will see building a web application using grunt before the start of the application.

1. To begin, under the project/ directory, construct a Scala object to extend PlayRunHook. We'll call it Grunt.scala:

import play.sbt.PlayRunHook
import sbt._
import scala.sys.process.Process

object Grunt {
  def apply(base: File): PlayRunHook = {

    object GruntProcess extends PlayRunHook {
    
      override def beforeStarted(): Unit = {
        Process("grunt dist", base).run
      }
    }

    GruntProcess
  }
}

 

2. Now, we will register the hook in build.sbt. 

PlayKeys.playRunHooks += Grunt(baseDirectory.value)

 

When you run sbt run, the grunt dist command will be executed in baseDirectory before the application is launched.

 

3. Now, we'll modify the Grunt.scala file we produced before to execute the grunt watch command to notice changes and rebuild the Web application when they occur.

import play.sbt.PlayRunHook
import sbt._
import java.net.InetSocketAddress
import scala.sys.process.Process

object Grunt {

  def apply(base: File): PlayRunHook = {

    object GruntProcess extends PlayRunHook {
    
      var watchProcess: Option[Process] = None

      override def beforeStarted(): Unit = {
        Process("grunt dist", base).run
      }

      override def afterStarted(): Unit = {
        watchProcess = Some(Process("grunt watch", base).run)
      }

      override def afterStopped(): Unit = {
        watchProcess.map(p => p.destroy())
        watchProcess = None
      }
    }

    GruntProcess
  }
}

 

4. Finally, when you start the application with sbt run, grunt watch will be executed to re-run the grunt build anytime files change.

Adding Compiler Options in Development Mode

In this part of the Sbt Cookbook, we will add a compiler to get more information on features warnings:

[info] Compiling 1 Scala source to ~/target/scala-2.10/classes...
[warn] there were 1 feature warnings; re-run with -feature for details

 

Now, add the -feature property to the scalacOptions attribute:

salacOptions += "-feature"

Adding Additional Asset Directory to Build System

In this part of the Sbt Cookbook, we will add a folder to build the system.

Suppose you want to add the pictures folder as an additional asset directory, then add the below line to build the system.

unmanagedResourceDirectories in Assets += baseDirectory.value / "pictures"

 

This will enable you to use routes.Assets.at with this folder.

Configuring Externalized Resources to Build System

In this part of the Sbt Cookbook, we will add the externalized resources to run the play development mode correctly.

In Play 2.4, the contents of the conf directory are automatically added to the classpath.

When packaging a Play application for production, the conf folder may reside in two locations:

The conf folder remains outside the jar file on the other system, so the content of the conf folder can be edited while restarting the application; any changes in the files can be immediately picked up. So to run the application correctly, the conf folder keep inside the jar file using the following command:

PlayKeys.externalizeResources := false

 

In play 2.7,  another configuration key has been added that allows you to block specific resources from being externalized, even if they are available.

PlayKeys.externalizeResources := true

PlayKeys.externalizeResourcesExcludes += baseDirectory.value / "conf" / "somefolder" / "somefile.xml"

Disable Documentation to Speed up the Compilation

In this part of the Sbt Cookbook, we will see the command to speed up the compilation by disabling documentation generation.

sources in (Compile, doc) := Seq.empty
publishArtifact in (Compile, packageDoc) := false

 

The first line disables documentation generation, while the second prevents the documentation artifact from being published.

Setting ivy logging level

By default, ivyLoggingLevel is set on UpdateLogging.DownloadOnly. This value can be modified using:

1. UpdateLogging.Quiet: It only displays the errors.

2. UpdateLogging.Full: It displays the full logs.

Suppose you want to only display errors:

ivyLoggingLevel := UpdateLogging.Quiet

Fork and Parallel Execution in Test

In this part of Sbt CookBook, we will see the command to disable the parallel execution enabled by default and enable fork execution.

This behavior can be altered by specifying parallelExecution and/or fork in Test:

parallelExecution in Test := true
fork in Test := false

Frequently Asked Questions

Frequently Asked Questions

How does Play Framework's routing work?

A router handles routing in Play Framework. The router is in charge of mapping incoming requests to the correct controller action. The router can be customized to handle several request types such as GET, POST, PUT, and DELETE. The router can also be customized to handle other URL types, including static URLs, dynamic URLs, and wildcard URLs.

How can one generate and return views on a request/response cycle?

Using the render() method in the Play Framework, you may produce and return views on a request/response cycle. This method renders a template and some data into HTML, which is subsequently returned to the client.

What are Akka Actors in the play framework?

The Play Framework employs Akka Actors as a concurrency paradigm. They are also called fault-tolerant, which means if any components fail, they can continue to function without any errors.

Conclusion

Congratulations on finishing the blog! We have discussed Cookbook in the play framework. We further look at the methods, compiler, additional assets, etc., to build an application system.

We hope this blog has helped you learn more about the Play framework. Here is a short list of our articles on Play Framework, please visit these blogs and increase your knowledge regarding Play.

 

Please refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please do upvote our blogs if you find them helpful and informative!

Happy learning!

thankyou

Live masterclass