Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
828 views
in Technique[技术] by (71.8m points)

scala - Play - How to use different configuration files for Dev/Prod?

I have a project with the following configuration

  • Commons.conf
  • Application.conf for development
  • Production.conf for production

Both application.conf and production.conf include commons.conf

I have to build the application using dist task to generate the zip file, then start it using the projectname.bat file provided.

I added javaOptions ++= Seq("-Dconfig.file=conf/production.conf") in build.sbt but it seem that did not working. I cannot launch the application on command line because i did want it to be done automatically. What's the better way to do separate .conf file configuration for development and production and have dist task taking the production ones?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On startup of your application, Play is automatically configured to load application.conf from the classpath. To override this with an alternative you need to specify this on startup.

Note that whether you are deploying to Windows and starting the app with a .bat, or Linux with a .sh you are usually executing that file as follows:-

$ target/universal/stage/bin/<project-name>

The above will execute your .bat file (if you are on Microsoft that is).

Specifying an alternative config file (couple of options)

So if you want to have separate configuration files for prod/dev then use the default application.conf for dev and use a production.conf for prod.

If you put application.conf inside conf then you would specify it as follows:-

$ /path/to/bin/<project-name> -Dconfig.resource=production.conf

If application.conf is not packaged with your app (ie. you have not put it in conf then you need to specify full path using.

$ target/universal/stage/bin/<project-name> -Dconfig.file=/full/path/to/conf/production.conf

An alternative approach (inheriting and overriding application.conf)

Include application.conf in prod.conf and just override what you need to for prod Note that you can include configuration inside another, so what I usually do is specify a prod.conf like this:-

include "application.conf"

key.to.override=blah

Then specify prod.conf when you start the app and application.conf will automatically be included. You can then just specify the properties in prod.conf that override those in application.conf for your production environment.

More

Not that you can also use system properties and environment variables to override settings in application.conf. I encourage you to take some time to read the docs to understand the options more fully.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...