Skip to content

Play Framework 2.x (Scala)

Overview

Clever Cloud supports Play! 2 applications natively. The following guide explains how to set up your application to run on Clever Cloud.

Configure your Scala + Play! 2 application

Mandatory configuration

  • Starting from Play 2.4, your application needs Java 8 to run. Please read select java version for more information.

Play! 2 applications use sbt. Please have a look at deploy Scala apps for a complete documentation on sbt deployment options.

Generate application secret

To deploy a Play! application you have to set a secret in your conf/application.conf the environment variable name depends on your Play2! version:

  • 2.6.x: play.http.secret.key=${?APPLICATION_SECRET}, to generate secret use sbt playGenerateSecret;
  • 2.4.x -> 2.5.x: play.crypto.secret=${?APPLICATION_SECRET} -> sbt playGenerateSecret;
  • 2.3.x: application.secret=${?APPLICATION_SECRET} -> sbt play-generate-secret.

Then, in your Clever Cloud application define APPLICATION_SECRET environment variable with the generated value.

Custom config file

If you don’t want to use the default conf/application.conf configuration file, pass another one to your application at startup with the CC_SBT_DEPLOY_ARGS environment variable: CC_SBT_DEPLOY_ARGS=-Dconfig.resource=clevercloud.conf. The former SBT_DEPLOY_GOAL name remains supported as an alias.

HTTPS support

HTTPS is handled by Clever Cloud ahead of your application, your application retrieves the traffic in plain http.

If you want to redirect HTTP traffic to HTTPS, please have a look at How to Redirect to HTTPS With Play 2.4.

HTTP support with Play! 2.x < 2.4

To be able to use request.secure, you have to add trustxforwarded=true in application.conf.

Multi-module project

If you have a single repository with multiple modules, then you can specify which module to run with CC_SBT_TARGET_DIR environment variable.

For instance, if your Sbt project contains a shared and play module and you want to execute the play module, then add CC_SBT_TARGET_DIR=play environment variable.

Monitor your application with New Relic

You can use New Relic to monitor your application on Clever Cloud.

Please refer to our New Relic documentation to configure it for your application.

Environment injection

Clever Cloud injects the environment variables of your application into its environment, along with those of its linked add-ons.

Custom build configurations

On Clever Cloud you can define some build configuration: like the app folder to deploy or the path to validate your application deployment is ready To do that follow the documentation here and add the environment variable you need.

Access environment variables from application.conf

To expose an environment variable through Play configuration, add my.option=${?MY_VARIABLE} to application.conf, then read the my.option configuration key from your application. You can also access it directly with System.getenv("MY_VARIABLE"), which returns null when the variable is not defined.

Known problems with Play! 2

sbt-plugin unresolved dependency error

If your project fails with the error sbt.ResolveException: unresolved dependency: play#sbt-plugin;2.0: not found this is because some versions of Play2 try to retrieve a nonexistent version of “sbt-plugin” which is required by the framework. You have two options to fix this problem:

  • You can set the “play.version” environment variable in the /clevercloud/sbt.json file. For example, for Play 2.0.4:
{
  "deploy": {
    "goal": "-Dplay.version=2.0.4"
  }
}
  • You can modify plugins.sbt in the project folder of your app like the following:
// Comment to get more information during initialization
logLevel := Level.Warn

// The Typesafe repository
resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"

// Use the Play sbt plugin for Play projects
addSbtPlugin("play" % "sbt-plugin" % "2.0.4") // The important part of the configuration

Both alternatives effectively fulfill the task at hand; you are free to select the one that best suits your preference.

More info on playframework.com.

Bad root server path error

The error occurs when we cannot find a bin to execute your project.

Failed to acquire connection: Too many connections

You may run into this error during deployments:

[error] c.j.b.h.AbstractConnectionHook - Failed to acquire connection to jdbc:<address> Sleeping for 1000ms and trying again. Attempts left: 10. Exception: null.Message:FATAL: too many connections for role "<user>"

By default, Play! opens a pool of 10 connections, which is more than the connection limit allowed by DEV database plans. During a no-downtime deployment, there are at least two instances of the application running in parallel, so it’s 20 connections.

To avoid connection exhaustion, you should limit your pool at half the number of available connections (if you have horizontal scaling enabled, adjust accordingly).

# conf/application.conf

db.default.partitionCount=2
db.default.maxConnectionsPerPartition=5
db.default.minConnectionsPerPartition=5

Go Further

Last updated on