如何声明2个自定义sbt设置,例如A和B,在Global
配置范围中定义B,其内容依赖于A,在几个配置范围中以不同的方式定义A,使得结果值虽然B只定义了一次,但每个配置中的B是不同的?
请考虑以下示例targetHost
,在remote
中的定义与在其他配置中的定义不同,scriptContent
取决于它:
object MyBuild extends Build {
lazy val remote = config("remote") describedAs ("configuration for remote environement ")
lazy val targetHost = settingKey[String]("private hostname of master server")
lazy val scriptContent = settingKey[String]("Some deployment script")
lazy val root: Project = Project("meme", file(".")).
settings(
name := "hello",
targetHost := "localhost",
targetHost in remote := "snoopy",
scriptContent := s"""
# some bash deployment here
/usr/local/uberDeploy.sh ${targetHost.value}
"""
)
}
我希望scriptContent
在两个配置范围中都有不同的值,但由于它取决于targetHost
范围内的Global
,因此其值始终相同:
> scriptContent
[info]
[info] # some bash deployment here
[info] /usr/local/uberDeploy.sh localhost
[info]
> remote:scriptContent
[info]
[info] # some bash deployment here
[info] /usr/local/uberDeploy.sh localhost
[info]
我希望获得以下内容:
> scriptContent
[info]
[info] # some bash deployment here
[info] /usr/local/uberDeploy.sh localhost
[info]
> remote:scriptContent
[info]
[info] # some bash deployment here
[info] /usr/local/uberDeploy.sh snoopy
[info]
答案 0 :(得分:0)
=>我们需要多次应用设置,每个范围一次,以强制重新评估scriptSetting
的内容:
import sbt._
import sbt.Keys._
object MyBuild extends Build {
lazy val remote = config("remote") describedAs ("configuration for remote environement ")
lazy val targetHost = settingKey[String]("private hostname of master server")
lazy val scriptContent = settingKey[String]("Some deployment script")
lazy val scriptSettings = Seq(
scriptContent := s"""
# some bash deployment here
/usr/local/uberDeploy.sh ${targetHost.value}
"""
)
lazy val root: Project = Project("meme", file("."))
.settings(
name := "hello",
targetHost := "localhost",
targetHost in remote := "snoopy"
)
.settings(scriptSettings: _*)
.settings(inConfig(remote)(scriptSettings) :_*)
}
产生预期结果:
> scriptContent
[info]
[info] # some bash deployment here
[info] /usr/local/uberDeploy.sh localhost
[info]
> remote:scriptContent
[info]
[info] # some bash deployment here
[info] /usr/local/uberDeploy.sh snoopy
[info]
>