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

2024-03-14

我正在定制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" }

为什么有这样的差别呢?


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"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐