在构建中未定义默认值时在命令行上设置设置值? [英] Setting value of setting on command line when no default value defined in build?

查看:39
本文介绍了在构建中未定义默认值时在命令行上设置设置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要特定设置的插件:configUrl

I am writing a plugin that requires a specific setting: configUrl

如果我在 build.conf 中指定该设置,它将如下所示:

If I specify that setting in my build.conf it would look like this:

MyPlugin.configUrl := "http://..../..."

然后我可以使用命令行来执行此操作:

I can then use the command line to do this:

sbt 'set MyPlugin.configUrl := "http://..../..."' performAction

有没有更好的方法可以让我在构建中不使用该设置?

Is there a better way that allows me to not have that setting in build?

如果我在没有该设置的情况下启动 sbt,则会出现以下错误:

If I start sbt without that setting I get the following error:

[error] Reference to undefined setting: 
[error] 
[error]   *:config-url from *:application-configuration

我搜索了谷歌,但找不到在命令行上提供设置的方法,如下所示:

I searched google but could not find a way to provide the setting on the command line, something like this:

sbt config-url="http://..../..."

推荐答案

如果设置有默认值,则应在插件中设置该默认值.如果不是,则类型应该是 Option[...],默认值为 None.通常,构建不应该要求从命令行传递参数只是为了加载.

If the setting has a default value, that default should be set in the plugin. If it does not, the type should probably be Option[...] with a default of None. Typically, a build should not require a parameter to be passed from the command line just to be loaded.

最后,如果主要是您不喜欢 set 语法,您可以使用系统属性并从您的设置中读取系统属性.但是,这仅限于字符串,因此您失去了类型安全性.

Finally, if it is primarily the set syntax you dislike, you can use system properties and read the system property from your setting. However, this is restricted to Strings and so you lose type safety.

系统属性可以通过 -Dkey=value 在命令行(直接或在您的启动脚本中)设置:

System properties can be set by -Dkey=value on the command line (either directly or in your startup script):

sbt -Dconfig.url=http://...

(根据您的 shell 的需要引用).将此值拉入设置:

(quoting as needed by your shell). To pull this value into the setting:

MyPlugin.configUrl := 
  url(System.getProperty("config.url", "<default>"))

其中 "" 是未设置系统属性时使用的值.如果您采用 Option 方法,

where "<default>" is the value to use if the system property is not set. If you take the Option approach,

MyPlugin.configUrl := 
  for(u <- Option(System.getProperty("config.url"))) yield
    url(u)

这篇关于在构建中未定义默认值时在命令行上设置设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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