播放 - 如何为 Dev/Prod 使用不同的配置文件?
Play - How to use different configuration files for Dev/Prod?
我有一个配置如下的项目
Commons.conf
Application.conf
发展
Production.conf
生产
application.conf
和 production.conf
都包括 commons.conf
我必须使用 dist 任务构建应用程序以生成 zip 文件,然后使用提供的 projectname.bat
文件启动它。
我在 build.sbt 中添加了 javaOptions ++= Seq("-Dconfig.file=conf/production.conf")
,但似乎没有用。我无法在命令行上启动应用程序,因为我确实希望它自动完成。为开发和生产单独配置 .conf
文件并让 dist
任务承担生产任务的更好方法是什么?
您可以通过两种方式进行:
- 如果您的软件包包含配置文件:
$ <path_to_your_executable_file> -Dconfig.resource=<name_of_your_conf_file>
这将在应用程序类路径中搜索配置文件,并且所有此类文件在打包前都应位于conf
目录中。
- 如果您想提供一个完全替代的文件,该文件未随应用程序一起打包:
$ <path_to_your_executable_file> -Dconfig.file=<path_to_your_conf_file>
如果您查看 zip 文件的内容,您会看到 bin
和 conf
目录。 bin
包含可执行文件,conf
包含配置文件。所以注意配置文件是单独打包的。
在您的应用程序启动时,Play 会自动配置为从类路径加载 application.conf
。要用替代方案覆盖它,您需要在启动时指定它。
请注意,无论您是部署到 Windows 并使用 .bat
启动应用程序,还是使用 .sh
启动应用程序,您通常都按如下方式执行该文件:-
$ target/universal/stage/bin/<project-name>
以上将执行您的 .bat
文件(如果您使用的是 Microsoft)。
指定替代配置文件(几个选项)
因此,如果您想为 prod/dev 使用单独的配置文件,请为开发使用默认的 application.conf
,为生产使用 production.conf
。
如果你把 application.conf
放在 conf
里面,那么你会指定它如下:-
$ /path/to/bin/<project-name> -Dconfig.resource=production.conf
如果 application.conf
没有与您的应用程序一起打包(即您没有将它放在 conf
中,那么您需要使用指定完整路径。
$ target/universal/stage/bin/<project-name> -Dconfig.file=/full/path/to/conf/production.conf
另一种方法(继承和覆盖 application.conf)
在 prod.conf
中包含 application.conf
并覆盖您需要的产品
请注意,您可以在另一个内部包含配置,所以我通常做的是像这样指定一个 prod.conf
:-
include "application.conf"
key.to.override=blah
然后在启动应用程序时指定 prod.conf
,application.conf
将自动包含在内。然后,您可以只在 prod.conf
中指定属性,以覆盖生产环境中 application.conf
中的属性。
更多
并不是说您也可以使用系统属性和环境变量来覆盖 application.conf
中的设置。我鼓励您花一些时间阅读 the docs 以更全面地理解这些选项。
在 Play Framework 2.5.x 中,您可以 运行 使用 sbt
命令指定配置文件的项目:
$ sbt '; set javaOptions += "-Dconfig.file=conf/integration.conf"; run'
您可以启动测试:
$ sbt '; set javaOptions += "-Dconfig.file=conf/integration.conf"; test'
或使用testOnly
:
$ sbt '; set javaOptions += "-Dconfig.file=conf/integration.conf"; testOnly Integration*'
如果您使用激活器,那么使用:激活器运行 -Dconfig.file=conf/auxiliary.conf
如果您使用的是 sbt,请使用:sbt ';设置 javaOptions += "-Dconfig.file=conf/auxiliary.conf"; 运行'
我有一个配置如下的项目
Commons.conf
Application.conf
发展Production.conf
生产
application.conf
和 production.conf
都包括 commons.conf
我必须使用 dist 任务构建应用程序以生成 zip 文件,然后使用提供的 projectname.bat
文件启动它。
我在 build.sbt 中添加了 javaOptions ++= Seq("-Dconfig.file=conf/production.conf")
,但似乎没有用。我无法在命令行上启动应用程序,因为我确实希望它自动完成。为开发和生产单独配置 .conf
文件并让 dist
任务承担生产任务的更好方法是什么?
您可以通过两种方式进行:
- 如果您的软件包包含配置文件:
$ <path_to_your_executable_file> -Dconfig.resource=<name_of_your_conf_file>
这将在应用程序类路径中搜索配置文件,并且所有此类文件在打包前都应位于conf
目录中。
- 如果您想提供一个完全替代的文件,该文件未随应用程序一起打包:
$ <path_to_your_executable_file> -Dconfig.file=<path_to_your_conf_file>
如果您查看 zip 文件的内容,您会看到 bin
和 conf
目录。 bin
包含可执行文件,conf
包含配置文件。所以注意配置文件是单独打包的。
在您的应用程序启动时,Play 会自动配置为从类路径加载 application.conf
。要用替代方案覆盖它,您需要在启动时指定它。
请注意,无论您是部署到 Windows 并使用 .bat
启动应用程序,还是使用 .sh
启动应用程序,您通常都按如下方式执行该文件:-
$ target/universal/stage/bin/<project-name>
以上将执行您的 .bat
文件(如果您使用的是 Microsoft)。
指定替代配置文件(几个选项)
因此,如果您想为 prod/dev 使用单独的配置文件,请为开发使用默认的 application.conf
,为生产使用 production.conf
。
如果你把 application.conf
放在 conf
里面,那么你会指定它如下:-
$ /path/to/bin/<project-name> -Dconfig.resource=production.conf
如果 application.conf
没有与您的应用程序一起打包(即您没有将它放在 conf
中,那么您需要使用指定完整路径。
$ target/universal/stage/bin/<project-name> -Dconfig.file=/full/path/to/conf/production.conf
另一种方法(继承和覆盖 application.conf)
在 prod.conf
中包含 application.conf
并覆盖您需要的产品
请注意,您可以在另一个内部包含配置,所以我通常做的是像这样指定一个 prod.conf
:-
include "application.conf"
key.to.override=blah
然后在启动应用程序时指定 prod.conf
,application.conf
将自动包含在内。然后,您可以只在 prod.conf
中指定属性,以覆盖生产环境中 application.conf
中的属性。
更多
并不是说您也可以使用系统属性和环境变量来覆盖 application.conf
中的设置。我鼓励您花一些时间阅读 the docs 以更全面地理解这些选项。
在 Play Framework 2.5.x 中,您可以 运行 使用 sbt
命令指定配置文件的项目:
$ sbt '; set javaOptions += "-Dconfig.file=conf/integration.conf"; run'
您可以启动测试:
$ sbt '; set javaOptions += "-Dconfig.file=conf/integration.conf"; test'
或使用testOnly
:
$ sbt '; set javaOptions += "-Dconfig.file=conf/integration.conf"; testOnly Integration*'
如果您使用激活器,那么使用:激活器运行 -Dconfig.file=conf/auxiliary.conf
如果您使用的是 sbt,请使用:sbt ';设置 javaOptions += "-Dconfig.file=conf/auxiliary.conf"; 运行'