有keys,可能有不同的定义值scopes(项目、配置或任务)。
可以定义键,即使它不在任何范围内使用,也可以在任何范围内为键赋予值。后者并不意味着该值将由特定任务使用。这意味着你可以reusesbt 为您的目的声明的密钥。
你所做的就是声明一个新的taskKey
。您定义要调用的任务clean
。然后你定义cleanKeepFiles
in the scope新任务的值等于之前的值加上目标中的数据库目录。
该值设置正确,但clean
任务不会在您的任务范围内寻找它。
您可以验证一下:
> show cleanOutput::cleanKeepFiles
[info] List(/home/lpiepiora/Desktop/sbt/stack-overflow/q-24020437/target/.history, /home/lpiepiora/Desktop/sbt/stack-overflow/q-24020437/target/database)
此外,您还可以检查:
> inspect *:cleanKeepFiles
[info] Setting: scala.collection.Seq[java.io.File] = List(/home/lpiepiora/Desktop/sbt/stack-overflow/q-24020437/target/.history)
[info] Description:
[info] Files to keep during a clean.
[info] Provided by:
[info] {file:/home/lpiepiora/Desktop/sbt/stack-overflow/q-24020437/}q-24020437/*:cleanKeepFiles
[info] Defined at:
[info] (sbt.Defaults) Defaults.scala:278
[info] Dependencies:
[info] *:history
[info] Reverse dependencies:
[info] *:clean
[info] Delegates:
[info] *:cleanKeepFiles
[info] {.}/*:cleanKeepFiles
[info] */*:cleanKeepFiles
[info] Related:
[info] *:cleanOutput::cleanKeepFiles
您还可以看到 sbt 知道,您已将其设置在范围内*:cleanOutput::cleanKeepFiles
,它只是不使用它。
它会在哪里寻找它?你可以通过以下方式检查检查 the clean
task.
> inspect clean
[info] Task: Unit
[info] Description:
[info] Deletes files produced by the build, such as generated sources, compiled classes, and task caches.
// next lines are important
[info] Dependencies:
[info] *:cleanKeepFiles
[info] *:cleanFiles
您可以看到依赖项之一是*:cleanKeepFiles
, the *
表示全局配置。这意味着clean
任务将在该范围内查找设置。您可以将设置更改为:
cleanKeepFiles += target.value / "database"
这会将其设置在所使用的正确范围内clean
task.
Edit
有一个doClean http://www.scala-sbt.org/0.13.0/sxr/sbt/Defaults.scala.html#sbt.Defaults.doClean您可以重复使用的功能。鉴于此,您可以像这样定义清理任务:
val cleanKeepDb = taskKey[Unit]("Cleans folders keeping database")
cleanKeepDb := Defaults.doClean(cleanFiles.value, (cleanKeepFiles in cleanKeepDb).value)
cleanKeepFiles in cleanKeepDb += target.value / "database"