Play - 如何为 Dev/Prod 使用不同的配置文件? [英] Play - How to use different configuration files for Dev/Prod?

查看:32
本文介绍了Play - 如何为 Dev/Prod 使用不同的配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置如下的项目

  • Commons.conf
  • Application.conf 用于开发
  • Production.conf 用于生产

application.confproduction.conf 都包含 commons.conf

我必须使用 dist 任务构建应用程序以生成 zip 文件,然后使用提供的 projectname.bat 文件启动它.

我在 build.sbt 中添加了 javaOptions ++= Seq("-Dconfig.file=conf/production.conf") 但它似乎不起作用.我无法在命令行上启动应用程序,因为我确实希望它自动完成.为开发和生产做单独的 .conf 文件配置并让 dist 任务接管生产的更好的方法是什么?

解决方案

在您的应用程序启动时,Play 会自动配置为从类路径加载 application.conf.要使用替代方案覆盖它,您需要在启动时指定它.

请注意,无论您是部署到 Windows 并使用 .bat 启动应用程序,还是使用 .sh 启动应用程序,您通常都会按如下方式执行该文件:-

$ target/universal/stage/bin/

以上将执行您的 .bat 文件(如果您使用的是 Microsoft).

指定替代配置文件(几个选项)

因此,如果您想为 prod/dev 使用单独的配置文件,请对 dev 使用默认的 application.conf,对 prod 使用 production.conf.>

如果你把 application.conf 放在 conf 中,那么你可以指定如下:-

$/path/to/bin/-Dconfig.resource=production.conf

如果 application.conf 没有与你的应用打包在一起(即你没有把它放在 conf 中,那么你需要指定完整路径 using.

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

另一种方法(继承和覆盖 application.conf)

prod.conf 中包含 application.conf 并覆盖您需要的 prod请注意,您可以在另一个中包含配置,所以我通常做的是指定一个 prod.conf 像这样:-

包含application.conf"key.to.override=废话

然后在启动应用程序时指定prod.confapplication.conf 将自动包含在内.然后,您只需指定 prod.conf 中的属性,覆盖 application.conf 中用于您的生产环境的属性.

更多

并不是说您还可以使用系统属性和环境变量来覆盖 application.conf 中的设置.我鼓励您花一些时间阅读文档,以更全面地了解这些选项.

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?

解决方案

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.

这篇关于Play - 如何为 Dev/Prod 使用不同的配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆