Table of contents
1.
Introduction🧑🏻‍🎓
2.
Deploying Your Application🧐
3.
The Application Secret🤔
4.
Using the Dist Task 👩‍🏫
5.
The Native Packager👨‍🎓
5.1.
Build a server distribution🚀
5.1.1.
Minimal Debian settings
5.1.2.
Minimal RPM settings
5.2.
Including Additional Files in Your Distribution📁
6.
Play PID Configuration🕝
7.
Publishing to a Maven (or Ivy) Repository🧐
8.
Running a Production Server in Place👨‍🏫
8.1.
Running a test instance🧑‍💻
9.
Using the sbt Assembly Plugin💥
10.
Frequently Asked Questions
10.1.
What do you mean by the iterator in the play framework?
10.2.
What is the difference between an enumeration and an iterator interface?
10.3.
Can you describe the steps in building a new Scala application utilizing the Play framework?
10.4.
Are there any restrictions while using the play framework to create JSON objects? 
10.5.
What do you know about the play framework's asynchronous processing?
11.
Conclusion
Last Updated: Mar 27, 2024
Easy

Deploying Your Application

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction🧑🏻‍🎓

The process of installing, configuring, upgrading, and enabling a single program or group of apps that make a software system usable, such as facilitating a specific URL on a server, is known as application deployment.

Play- Deploying your application OG image

The following article will explore how we can deploy our application in play. Let’s go! 🫡

Deploying Your Application🧐

Deploying Your Application

The run command should not be used to execute an application in production mode, as we have shown how to do with play applications running in development mode. When using run, play checks with sbt to verify if any files have changed with each request, which may significantly affect the performance of your application.

We can deploy a play application in production mode in several ways. Let's begin by producing a distribution artifact, the method that is advised.

The Application Secret🤔

You must create an application secret before running your application in production mode. Configuring the application secret has more information on how to do this. The use of play is demonstrated in the examples that follow. 
http.secret.key=ad31779d4ee49d5ad5162bf1429c32e2e9933f3b. 
To utilize while deploying to production, you must generate your secret.

Using the Dist Task 👩‍🏫

Except for a java installation on the server, the dist task creates a binary version of the application that you can deploy to a server without relying on sbt.

In the play console, type dist:

[my-app] $ dist
You can also try this code with Online Java Compiler
Run Code


And you will find something similar to this:

$ sbt
[info] Loading global plugins from /Users/play-developer/.sbt/0.13/plugins
[info] Loading project definition from /Users/play-developer/my-app/project
[info] Set current project to my-app (in build file:/Users/play-developer/my-app/)
[my-app] $ dist
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-sources.jar ...
[info] Done packaging.
[info] Wrote /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT.pom
[info] Main Scala API documentation to /Users/play-developer/my-first-app/target/scala-2.13/api...
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-web-assets.jar ...
[info] Done packaging.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT.jar ...
[info] Done packaging.
model contains 21 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-javadoc.jar ...
[info] Done packaging.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-sans-externalized.jar ...
[info] Done packaging.
[info]
[info] Your package is ready in /Users/play-developer/my-first-app/target/universal/my-first-app-1.0-SNAPSHOT.zip
[info]
[success] Total time: 5 s, completed Feb 6, 2017 2:08:44 PM
[my-app] $
You can also try this code with Online Java Compiler
Run Code


This results in a ZIP file that contains all of the JAR files required to run your application in the target/universal folder.

On the target server, unzip the file before running the script in the bin directory to launch the application. The script has two versions: a Windows.bat script and a bash shell script with the same name as your program

$ unzip my-app-1.0.zip
$ my-app-1.0/bin/my-first-app -Dplay.http.secret.key=ad31779d4ee49d5ad5162bf1429c32e2e9933f3b
You can also try this code with Online Java Compiler
Run Code


From the command line, you can also supply a different configuration file for a production environment:

$ my-app-1.0/bin/my-app -Dconfig.file=/full/path/to/conf/application-prod.conf
You can also try this code with Online Java Compiler
Run Code


Start the script with the -h option to see a detailed usage description.

The dist process automatically includes the API documentation in the resulting package. Add these lines to build.sbt even if they are not required:

sources in (Compile, doc) := Seq.empty
publishArtifact in (Compile, packageDoc) := false
You can also try this code with Online Java Compiler
Run Code


The preceding statement must be applied to all sub-project definitions for builds containing sub-projects.

The Native Packager👨‍🎓

The Native Packager

The sbt Native Packager plugin is used by play. The dist task to build a zip file is declared by the native packager plugin. The following are directly equivalent to calling the dist task:

[my-app] $ universal:packageBin
You can also try this code with Online Java Compiler
Run Code


Other kinds of archives that can be created include:

💡tar.gz

💡OS X disk images

💡Microsoft Installer (MSI)

💡RPMs

💡Debian packages

💡System V / init.d and Upstart services in RPM/Debian packages.

Build a server distribution🚀

The sbt-native-packager plugins offer a variety of archetypes. The java server pattern, which play by default employs, makes it possible to use the following features:

System V or Upstart startup scripts

Default folders

Minimal Debian settings

Include the following construction settings:

lazy val root = (project in file("."))
  .enablePlugins(PlayScala, DebianPlugin)

maintainer in Linux := "Coding Ninjas <Coding.Ninjas@example.com>"

packageSummary in Linux := "My Coding Ninjas package summary"

packageDescription := "My Coding Ninjas package description"
You can also try this code with Online Java Compiler
Run Code


Then build your package with:

[my-app] $ debian:packageBin
You can also try this code with Online Java Compiler
Run Code


Minimal RPM settings

Add the following settings to your build:
lazy val root = (project in file("."))
  .enablePlugins(PlayScala, RpmPlugin)

maintainer in Linux := "Coding Ninjas <Coding.Ninjas@example.com>"

packageSummary in Linux := "My Coding Ninjas package summary"

packageDescription := "My lCoding Ninjas package description"

rpmRelease := "1"

rpmVendor := "example.com"

rpmUrl := Some("http://github.com/example/server")

rpmLicense := Some("Apache v2")
You can also try this code with Online Java Compiler
Run Code


Then build your package with:

[my-app] $ rpm:packageBin
You can also try this code with Online Java Compiler
Run Code

Including Additional Files in Your Distribution📁

Anything contained in the dist directory of your project will be made available in the distribution created by the native packager. You should note that the src/universal directory specified in the native packager's documentation has a counterpart in play's dist directory.

Play PID Configuration🕝

The Production setup describes how Play manages its own PID.

play uses a different pidfile; therefore, we must provide it with the correct path, which in this case is packageName.value. The only acceptable pid file name is play.pid. Place a file called application.ini within the dist/conf folder and add the following information to it so the startup script will know where to look for the PID file:

s"-Dpidfile.path=/var/run/${packageName.value}/play.pid",
# Add all other startup settings here, too
You can also try this code with Online Java Compiler
Run Code


To prevent play from creating a PID just set the property to /dev/null:

-Dpidfile.path=/dev/null
You can also try this code with Online Java Compiler
Run Code

Publishing to a Maven (or Ivy) Repository🧐

Your application can also be uploaded to a Maven repository. This publishes both the POM file and the JAR file containing your application.

In your build.sbt file, you must configure the repository you wish to publish to:

publishTo := Some(
  "My resolver".at("https://CodingNinjas.com/repo")
)

credentials += Credentials(
  "Repo",
  "https://CodingNinjas.com/repo",
  "admin",
  "admin123"
)
You can also try this code with Online Java Compiler
Run Code


Then in the play console, use the publish task:

[my-app] $ publish
You can also try this code with Online Java Compiler
Run Code

Running a Production Server in Place👨‍🏫

Running a production server

In some cases, you might prefer to launch your application directly from the source directory of your project rather than producing a complete distribution. The stage task can be used to do the necessary sbt installation on the server.

$ sbt clean stage
You can also try this code with Online Java Compiler
Run Code


You will see something similar to this:

$ sbt
[info] Loading global plugins from /Users/play-developer/.sbt/0.13/plugins
[info] Loading project definition from /Users/play-developer/my-first-app/project
[info] Set current project to my-first-app (in build file:/Users/play-developer/my-first-app/)
[my-first-app] $ stage
[info] Updating {file:/Users/play-developer/my-first-app/}root...
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-sources.jar ...
[info] Done packaging.
[info] Wrote /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT.pom
[info] Resolving jline#jline;2.12.2 ...
[info] Done updating.
[info] Main Scala API documentation to /Users/play-developer/my-first-app/target/scala-2.13/api...
[info] Compiling 8 Scala sources and 1 Java source to /Users/play-developer/my-first-app/target/scala-2.13/classes...
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-web-assets.jar ...
[info] Done packaging.
model contains 21 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-javadoc.jar ...
[info] Done packaging.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT.jar ...
[info] Done packaging.
[info] Packaging /Users/play-developer/my-first-app/target/scala-2.13/my-first-app_2.13-1.0-SNAPSHOT-sans-externalized.jar ...
[info] Done packaging.
[success] Total time: 8 s, completed Feb 6, 2017 2:11:10 PM
[my-app] $
You can also try this code with Online Java Compiler
Run Code


Your application is cleaned up, compiled, and the necessary dependencies are located and copied to the target/universal/stage directory. Additionally, it generates a bin/start script, where start stands for the project name. There is a similar bat file for Windows, and the script operates the play server for Unix-style systems.

For instance, to launch the project my-first-application app's from the project folder, you can:

$ target/universal/stage/bin/my-first-app -Dplay.http.secret.key=ad31779d4ee49d5ad5162bf1429c32e2e9933f3b
You can also try this code with Online Java Compiler
Run Code


From the command line, you can also supply a different configuration file for a production environment:

$ target/universal/stage/bin/my-first-app -Dconfig.file=/full/path/to/conf/application-prod.conf
You can also try this code with Online Java Compiler
Run Code

Running a test instance🧑‍💻

The play offers a practical tool for launching a test application in production mode.

Run runProd to launch a program in production mode.

[my-app] $ runProd
You can also try this code with Online Java Compiler
Run Code

Using the sbt Assembly Plugin💥

The sbt assembly plugin can also be used to package and run Play applications even though it is not officially supported. One jar will be created as an output artifact, and you'll be able to run it using the java command.

To use it, include this dependency in your project's plugins.sbt file:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")
You can also try this code with Online Java Compiler
Run Code


Add the following configuration now to your build.sbt file:

mainClass in assembly := Some("play.core.server.ProdServerStart")
fullClasspath in assembly += Attributed.blank(PlayKeys.playPackageAssets.value)

assemblyMergeStrategy in assembly := {
  case manifest if manifest.contains("MANIFEST.MF") =>
  
    MergeStrategy.discard
  case referenceOverrides if referenceOverrides.contains("reference-overrides.conf") =>
    // Keep the content for all reference-overrides.conf files
    MergeStrategy.concat
  case x =>
    // For all the other files, use the default sbt-assembly merge strategy
    val oldStrategy = (assemblyMergeStrategy in assembly).value
    oldStrategy(x)
}
You can also try this code with Online Java Compiler
Run Code


Now that the artifact has been built, you may launch your application by typing:

$ java -Dplay.http.secret.key=ad31779d4ee49d5ad5162bf1429c32e2e9933f3b -jar target/scala-2.XX/<yourprojectname>-assembly-<version>.jar
You can also try this code with Online Java Compiler
Run Code


Of course, you'll need to change the project name, version, and scala version.

We hope you understood everything about deploying your application in the play framework.🤗

Frequently Asked Questions

What do you mean by the iterator in the play framework?

The iterator interface provides an alternative method for iterating through any collection. So, getting an iterator instance from a collection is simple using the iterator technique. In the Java collection framework, the iterator replaces enumeration. The caller may also delete components from the underlying collection as the iteration is happening.

What is the difference between an enumeration and an iterator interface?

A relatively small amount of memory is consumed by an enumeration, which is twice as quick as an iterator. Very fundamental and appropriate use of enumeration is for meeting basic needs. However, the Iterator is more secure than enumeration since it never permits other threads to alter the group object while it is being iterated. Iterators allow for the destruction of underlying elements, which is not feasible with enumeration.

Can you describe the steps in building a new Scala application utilizing the Play framework?

You must use Scala's "activator" command to start a new application utilizing the play framework. This will produce a few files that you will need to change to customize your application and a new directory structure for your project.

Are there any restrictions while using the play framework to create JSON objects? 

There are indeed some restrictions. For instance, you cannot utilize the pay framework to produce a JSON object with duplicate keys.

What do you know about the play framework's asynchronous processing?

play framework's asynchronous processing features let you handle several requests concurrently. This can be helpful if you need to handle many requests rapidly or if you don't want to block one request while you wait for another to complete.

Conclusion

This article taught us how to deploy an application in the play framework. We began with a brief introduction to the method, followed by a detailed code example.

After reading about deploying an application in the play framework, refer to play documentationplay Framework WikipediaIntroduction to play framework in Java, and 15 Best Java frameworks to use in 2022-Coding Ninjas or a deeper understanding of Play development and other related topics.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Live masterclass