如何依赖"当前"配置

时间:2015-06-15 13:40:04

标签: scala sbt

如何声明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] 

1 个答案:

答案 0 :(得分:0)

发现它!我的问题实际上是重复的(抱歉......),最相关的答案就在这里:How can i make an SBT key see settings for the current configuration?

=>我们需要多次应用设置,每个范围一次,以强制重新评估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]
>