如何排除自定义清理任务中的文件?

时间:2014-06-03 16:36:07

标签: sbt

我正在开发一个自定义TaskKey,该clean会将target保存在lazy val cleanOutput = taskKey[Unit]("Prints 'Hello World'") cleanOutput := clean.value cleanKeepFiles in cleanOutput <+= target { target => target / "database" } 目录中的文件夹中 - 它是一个我不知道的数据库想要每次都填补。

所以我尝试了类似的东西:

in cleanOutput

似乎没有考虑声明cleanKeepFiles in clean <+= target { target => target / "database" } 即使我只做以下事情,它也不起作用:

cleanKeepFiles <+= target { target => target / "database" }

但是以下工作:

{{1}}

为什么会有区别?

1 个答案:

答案 0 :(得分:3)

,它们可能在不同的范围(项目,配置或任务)中定义了值。

可以定义密钥,即使它未在任何范围内使用,也可以在任何范围内为密钥赋值。后者并不意味着该值将由特定任务使用。这意味着您可以重用为sbt声明的密钥。

你宣告新的taskKey。您定义要调用clean的任务。然后,在新任务的范围中定义cleanKeepFiles,使其等于之前的值,加上目标中的数据库目录。

值已正确设置,但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中设置它,它只是没有使用它。

它会在哪里寻找它?您可以通过检查 clean任务来检查它。

> 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*表示全局配置。这意味着clean任务将查找该范围内的设置。您可以将设置更改为:

cleanKeepFiles += target.value / "database"

这会将其设置在clean任务使用的正确范围内。

修改

您可以重复使用doClean功能。鉴于此,您可以像这样定义清洁任务:

val cleanKeepDb = taskKey[Unit]("Cleans folders keeping database")

cleanKeepDb := Defaults.doClean(cleanFiles.value, (cleanKeepFiles in cleanKeepDb).value)

cleanKeepFiles in cleanKeepDb += target.value / "database"