Jerry Mindek
Jerry Mindek

Reputation: 523

Detect Play 2.2.x mode prior to start of Application using Java

I want to point my play application to a particular application config file based on the environment it is running in. There are three and they correspond to the standard Play states:

A co-worker shared a method for doing this which requires setting an OS environment variable.

I'd like to eliminate the need to set an OS variable. My preference is the use whatever Play uses at startup to know what mode it is in.

For example, if you execute play run from the command line, part of the output is "[info] play - Application started (Dev)"

I want to use this information in my Global.java where I override onLoadConfig like so:

public Configuration onLoadConfig(Configuration baseConfiguration, File f, ClassLoader loader) {
    String playEnv=<some static method to get mode>;        
    Config additionalConfig = ConfigFactory.parseFile(new File(f,"conf/application."+playEnv+".conf"));
    Config baseConfig = baseConfiguration.getWrappedConfiguration().underlying();
    return new Configuration(baseConfig.withFallback(additionalConfig));
}

Everything that I find is how to do this after the application has been started i.e. using isDev(), isTest(), isProd().

Is there static method that provides the mode while I am overriding onLoadConfig in Global.java?

Upvotes: 12

Views: 2411

Answers (3)

AlexV
AlexV

Reputation: 562

The issue appeared to be addressed in the latest Play (3.0.0). There is another onLoadConfig method added to Global witch has a mode: {Dev,Prod,Test} parameter.

public Configuration onLoadConfig(Configuration config, File path, ClassLoader classloader, Mode mode) {
    return onLoadConfig(config, path, classloader); // default implementation
}

Upvotes: 3

Will Sargent
Will Sargent

Reputation: 4396

I think play run is dev, play start is prod.

EDIT: If you're looking to see what the current mode is, it's passed in through play.api.Play.current:

Play.current.mode match {
  case Play.Mode.Dev => "dev"
  case Play.Mode.Prod => "prod"
}

Upvotes: 3

biesior
biesior

Reputation: 55798

Play allows to specifying alternative configuration file with command line so no need for setting OS variables.

You can of course create some bash script / bat file to avoid writing it every time

Upvotes: 0

Related Questions