How to detect application mode in Play 2.x

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())

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.
 */

In play 2.3.X you can also check via:

play.Play.isProd()
play.Play.isDev()
play.Play.isTest()

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.