Setting value of setting on command line when no default value defined in build?

老子叫甜甜 提交于 2020-01-11 09:36:11

问题


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

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?

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://..../..."

回答1:


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.

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.

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

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

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

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

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)


来源:https://stackoverflow.com/questions/16939554/setting-value-of-setting-on-command-line-when-no-default-value-defined-in-build

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!