我正在研究一个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" }
为什么有区别?
有一些键,它们可能在不同的范围(项目,配置或任务)中具有定义的值。
即使没有在任何范围内使用键,也可以定义键,并且可以在任何范围内给键赋值。后者并不意味着该值将由特定任务使用。这意味着您可以出于自己的目的重复使用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知道,您已经在scope中设置了它,*: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"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句