How to detect application mode in Play 2.x
Asked Answered
L

6

34

From within a Play 2.1 application, how would I programmatically determine which mode the application is running in (i.e., Development vs. Production)?

For example, it would be useful to be able to do something like this from inside a template:

<p>@if(__some_play_API_call__ == Dev) { <b>Development mode</b> }</p>

In the Play 2.0 API documentation, there appears to be a mode property of the play.api.Application class... however, I am unsure about how to get at the instance of the currently running application.

Lysippus answered 30/1, 2013 at 20:31 Comment(0)
R
49

You can access the current Appliction via

play.api.Play.current()

to find out the mode try

play.api.Play.current().mode()

or simply use

play.api.Play.isDev(play.api.Play.current())
Rift answered 30/1, 2013 at 20:40 Comment(1)
In play 2.4.2 I had to use play.api.Play.current instead of play.api.Play.current()Mcglone
T
19

In Play 2.5.x the play.Play.isDev() method is deprecated, one has to use dependency injection:

import javax.inject.Inject;

public class Example {

    @Inject
    private play.Environment environment;

    public void myMethod() {
        if (environment.isDev()) {
          ...
        }
    }
}

Or equivalently in Scala:

class ErrorHandler @Inject()(environment: Environment) {

  def myMethod() = {
    if (environment.isDev) {
      ...
    }
  }

}

environment.isDev() returns a Boolean, which one can easily pass to a template. No need to use implicit variables as described here.

Tirewoman answered 7/8, 2016 at 16:52 Comment(2)
This answer is correct, but god this is needlessly verbose. Why is play promoting an unsafe and verbose pattern like dependency injection ?Challenging
@Tirewoman Where do we initialize the environment variable? When I do @Inject private play.Environment environment; the environment here throws a NullPointerException, when I try to do environment.isProd( ). I am using JAVAIchthyo
D
13

With Play 2.5, Play 2.6 and Play 2.7

You can do it like this:

import play.Environment

class MyController @Inject()(env: Environment) {

  println(s"DevMode is ${env.isDev}")
  println(s"ProdMode is ${env.isProd}")
  println(s"TestMode is ${env.isTest}")

}

Or in Play 2.6 and Play 2.7 you have also the version with play.api.Environment:

import play.api.Environment

class MyController @Inject()(env: Environment) {

  println(s"ProdMode is ${env.mode == Mode.Prod}")
  println(s"DevMode is ${env.mode == Mode.Dev}")
  println(s"TestMode is ${env.mode == Mode.Test}")
}

For both the Scala Doc states:

/**
 * The environment for the application.
 *
 * Captures concerns relating to the classloader and the filesystem for the application.
 */
Devitrify answered 7/3, 2018 at 10:57 Comment(2)
Actually these no longer work in Play 2.6 (tested on 2.6.20). Use env.mode instead.Royceroyd
@Royceroyd See my adjusted response - seems there are 2 possibilities for the same;)Devitrify
C
10

In play 2.3.X you can also check via:

play.Play.isProd()
play.Play.isDev()
play.Play.isTest()
Champac answered 22/10, 2015 at 13:52 Comment(2)
how does play know if it is in Dev or Prod?Forrest
It all depends on the way you start the application. More details here: playframework.com/documentation/2.4.x/ProductionChampac
A
5

In Play 2.5 using Scala there is a context.environment.mode value of Enumeration from play.api.Mode with one of the values Dev, Test, Prod.
For compile time dependency injection you have context available in your app loader and if you extend BuiltInComponentsFromContext then you can use (inject) directly environment.mode

Adai answered 21/8, 2016 at 15:0 Comment(0)
R
0

In Play 2.6, inject an Environment instance and use its mode field: one of play.api.Mode enum values.

import javax.inject.Inject
import play.api.Environment
import play.api.Mode.Prod
import play.api.mvc.{AbstractController, ControllerComponents}

class TestController @Inject()(cc: ControllerComponents, env: Environment)
    extends AbstractController(cc) {

  def hello = Action {
    if (env.mode == Prod) {
      // ...
    }

    Ok(s"Hello world in ${env.mode} mode")
  }

}

At least in Play 2.6.20, the methods env.isDev, env.isProd, etc, mentioned by pme, no longer work.

Royceroyd answered 11/10, 2018 at 8:31 Comment(1)
The question is how to do that inside a template, not in a controllerHodgkins

© 2022 - 2024 — McMap. All rights reserved.