简体   繁体   English

SBT-如何基于外部源向application.conf文件添加/修改值

[英]SBT - How can I add/modify values to application.conf file based on an external source

I read that SBT has functionality to generate source code and resource files . 我读到SBT具有生成源代码和资源文件的功能

In my case I want to add/modify a field in an application.conf file during compilation/packaging of the project (leaving the others in place) 就我而言,我想在项目的编译/打包过程中在application.conf文件中添加/修改一个字段(将其他字段留在原处)

For instance my application.conf file has something like: 例如,我的application.conf文件具有以下内容:

A {
  B = "Some Value"
  C = "Some value to be modified"
}

I would like in the SBT to read an external file and change or add the value of AB or AC 我想在SBT中读取外部文件并更改或添加AB或AC的值

So if it is possible to do something along the lines of: 因此,如果可以按照以下方式做点事情:

build.sbt

lazy val myProject = project.in(file('myproject')
// pseudo code - How do I do this?
.sourceGenerators in Compile += "Read file /path/to/external/file and add or replace the value of application.conf A.B = some external value"

You can replace the values with environment variable values provided while compiling / building your project. 您可以在编译/构建项目时用提供的环境变量值替换这些值。 For that you'd have to 为此,您必须

A {
  B = "Some Value"
  B = ${?B_ENV}
  C = "Some value to be modified"
  C = ${?C_ENV} 
}

Where B_ENV and C_ENV are the environment variables you set in your terminal either before build or within the build command (before it) 其中B_ENVC_ENV是您在构建之前或在构建命令中(在此之前)在终端中设置的环境变量

$ B_ENV=1 C_ENV=2 sbt run

Source: https://www.playframework.com/documentation/2.6.x/ProductionConfiguration#using-environment-variables 来源: https : //www.playframework.com/documentation/2.6.x/ProductionConfiguration#using-environment-variables

In this case you can do without sbt and this approach would also work with maven or cradle . 在这种情况下,您可以不使用sbt并且这种方法也适用于mavencradle

The *.conf support orignates from typesafe config ( https://github.com/lightbend/config ). *.conf支持来自typesafe confighttps://github.com/lightbend/config )。

There is a feature to get environment variables to be used in the configuration which should be a good fit to solve the problem. 有一个功能可以使环境变量在配置中使用,这很适合解决问题。

There are two approaches I would suggest to use 我建议使用两种方法

1.) Fail on missing configuration 1.)缺少配置失败

If configuration of this vallue is important and to prevent the deplyment of misconfigurated application the startup should fail on missing environment variables. 如果此方法的配置很重要,并且要防止出现配置错误的应用程序,则启动会因缺少环境变量而失败。

in application.conf application.conf

key=${TEST} // expects "TEST" to be set, fails otherwise

2.) Hardcoded value with override 2.)具有覆盖的硬编码值

If there is a sensible default behaviour that only in some circumstances should be changed. 如果存在明智的默认行为,则仅在某些情况下应更改。

in application.conf application.conf

key="test" // hardcoded key
key=${?TEST} // override "key" with 3nv "$TEST" value, when it is given

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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