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

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

问题描述

我有一个具有以下配置的项目

I have a project with the following configuration

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

application.confproduction.conf都包含commons.conf

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

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

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

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?

推荐答案

在应用程序启动时,系统会自动将Play配置为从类路径加载application.conf.要用其他方法覆盖它,您需要在启动时指定它.

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.

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

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>

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

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

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

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.

如果将application.conf放在conf内,则应按以下方式指定它:-

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

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

如果application.conf未与您的应用程序打包在一起(即您尚未将其放在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

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

prod.conf中包含application.conf,只是覆盖生产所需的内容 请注意,您可以在另一个内部包含配置,因此我通常要做的是指定prod.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

然后在启动应用程序时指定prod.conf,并且将自动包含application.conf.然后,您可以仅在prod.conf中指定属性,以覆盖生产环境中的application.conf中的属性.

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.

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

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.

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

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