为什么我对`ThisBuild`的使用不起作用?

时间:2015-05-20 04:52:11

标签: plugins sbt settings

我写了一个简单的SBT插件,它定义了一个helloMessage,默认值为None

lazy val helloMessage = settingKey[Option[String]]("the message for hello")

override def projectSettings = Seq(
  helloMessage in ThisBuild := None,
  hello := println("Hello from my plugin: " + helloMessage.value)
)

然后在测试项目中,我添加了这个插件,并在build.sbt中定义:

helloMessage in ThisBuild := Some("hello from this build")

lazy val root = project in file(".")

lazy val core = project in file("core")

您可能会注意到ThisBuild使用了helloMessage

但问题是,当我在测试项目中运行./sbt helloMessage时,它只输出None!不是我定义的消息Some("hello from this build")

但如果删除这两行:

lazy val root = project in file(".")

lazy val core = project in file("core")

或只是core行,它会输出预期的消息Some("hello from this build")

哪里错了?如果我必须保留多个项目,如何修复它?

3 个答案:

答案 0 :(得分:0)

首先按照建议in the docs使用buildSettings进行in ThisBuild设置。

答案 1 :(得分:0)

可能的解决方案,改变:

helloMessage in ThisBuild := None

helloMessage in Global := None

答案 2 :(得分:0)

我认为问题在于初始化顺序。在build.sbt文件中,将首先执行行helloMessage in ThisBuild := Some("hello from this build")。这将在全局(构建)范围中设置helloMessage设置。

现在,当您的任何项目初始化时,它将运行新设置helloMessage in ThisBuild := None。这将覆盖全局范围中的helloMessage设置为None

解决此问题的一种方法是从项目设置中删除ThisBuild。也就是说,您将ThisBuild范围保留在主build.sbt文件中,但将本地范围(helloMessage := None)保留在插件定义中的项目设置中。

相关问题