简体   繁体   中英

How do I specify a config file with play 2.4 and activator

I am building a Scala Play 2.4 application which uses the typesafe activator.

I would like to run my tests 2 times with a different configuration file for each run.

How can I specify alternative config files, or override the config settings?

I currently run tests with the command "./activator test"

You can create different configuration files for different environments/purposes. For example, I have three configuration files for local testing, alpha deployment, and production deployment as in this project https://github.com/luongbalinh/play-mongo

You can specify the configuration for running as follows:

activator run -Dconfig.resource=application.conf

where application.conf is the configuration you want to use.

You can create different configuration files for different environments. To specify the configuration to use it with activator run, use the following command:

activator "run -Dconfig.resource=application.conf"

where the application.conf is the desired configuration. Without the quotes it did not work for me. This is using the same configuration parameters as you use when going into production mode as described here: https://www.playframework.com/documentation/2.5.x/ProductionConfiguration#Specifying-an-alternate-configuration-file

Important to know is also that config.resource tries to locate the configuration within the conf/ folder, so no need to specify that as well. For full paths not among the resources, use config.file. Further reading is also in the above link.

The quotes need to be used because you do not want to send the -D to activator, but to the run command. Using the quotes, the activator's JVM gets no -D argument but it interprets "run -Dconfig.file=application.conf" and sets the config.file property accordingly, also in the activator's JVM.

This was already discussed here: Activator : Play Framework 2.3.x : run vs. start

The below command works with Play 2.5

 $ activator -Dconfig.resource=jenkins.conf run

https://www.playframework.com/documentation/2.5.x/ProductionConfiguration

Since all the above are partially incorrect, here is my hard wrought knowledge from the last weekend.

  1. Use include "application.conf" not include "application" (which Akka does)
  2. Configs must be named .conf or Play will discard them silently
  3. You probably want -Dconfig.file=<file>.conf so you're not classpath dependent
  4. Make sure your provide the full file path (eg /opt/configs/prod.conf )

Example

Here is an example of this we run:

#prod.conf
include "application"

akka.remote.hostname = "prod.blah.com"    

# Example of passing in S3 keys
s3.awsAccessKeyId="YOUR_KEY"
s3.awsSecretAccessKey="YOUR_SECRET_KEY"

And just pass it in like so:

activator -Dconfig.file=/var/lib/jenkins/jenkins.conf test

of if you fancy SBT:

sbt -Dconfig.file=/var/lib/jenkins/jenkins.conf test

Dev Environment

Also note it's easy to make a developer.conf file as well, to keep all your passwords/local ports, and then set a .gitignore so dev's don't accidentally check them in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM