如何在自定义清理任务中排除文件? [英] How to exclude files in a custom clean task?

查看:41
本文介绍了如何在自定义清理任务中排除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个自定义 TaskKey,它使 clean 保持在 target 目录中的文件夹 - 这是一个我不知道的数据库不想每次都填.

I'm working on a custom TaskKey that makes a clean that keeps a folder in target directory - it's a database that I don't want to fill everytime.

所以我尝试了类似的方法:

So I tried something like that:

lazy val cleanOutput = taskKey[Unit]("Prints 'Hello World'")

cleanOutput := clean.value

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

似乎没有考虑到在cleanOutput中的声明.
即使我只执行以下操作,它也不起作用:

It seems that the statement in cleanOutput is not taken in consideration.
Even when I do just the following it doesn't work:

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

但以下有效:

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

为什么会有不同?

推荐答案

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

There are keys, which may have defined values in different scopes (project, configuration or task).

键可以被定义,即使它没有在任何范围内使用,并且键可以在任何范围内被赋予一个值.后者并不意味着该值将由特定任务使用.这意味着您可以重用 sbt 声明的密钥用于您的目的.

A key can be defined, even if it's not used in any scope, as well as a key can be given a value in any scope. The later doesn't mean that the value will be used by a specific task. It means you can reuse keys declared by sbt for your purposes.

你做什么你声明一个新的taskKey.你定义你的任务来调用 clean.然后在新任务的 范围 中定义 cleanKeepFiles 等于之前的任何值,加上目标中的数据库目录.

What you do you declare a new taskKey. You define your task to call clean. You then define cleanKeepFiles in the scope of your new task to equal to whatever the value was before, plus your database directory in the target.

该值设置正确,但 clean 任务不会在您的任务范围内查找它.

The value is set correctly, but the clean task will not look for it in the scope of your task.

你可以验证一下:

> 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 范围内,它只是没有使用它.

You can also see that sbt knows, that you have set it in the scope *:cleanOutput::cleanKeepFiles, it is just not using it.

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

Where it will look for it?. You can check it by inspecting 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* 表示全局配置.这意味着 clean 任务将在该范围内查找设置.您可以将设置更改为:

You can see that one of the dependencies is *:cleanKeepFiles, the * means Global configuration. This means that the clean task will look for the setting in that scope. You can change your setting to:

cleanKeepFiles += target.value / "database"

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

This will set it in the correct scope used by the clean task.

有一个 doClean 可以重用的函数.鉴于此,您可以像这样定义您的清理任务:

There is a doClean function which you can reuse. Given that, you can define your clean task like this:

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

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

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

这篇关于如何在自定义清理任务中排除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆