Add additional directory to clean task in SBT buil

2020-03-15 01:36发布

问题:

I have a SBT build where the tests create temporary files into a directory called temp. How can I tell SBT to delete this folder when I call the clean task?

回答1:

Use this setting in the project containing the temp directory:

cleanFiles <+= baseDirectory { base => base / "temp" }

This adds the "temp" directory to the list of files to recursively delete when clean runs.

The < means "configure in terms of other tasks/settings", the + means append to the current list of files, and baseDirectory is the setting providing the base directory of the project.

You can see how clean is configured using the inspect command, documented in more detail on the Inspecting Settings page. An edited sbt session shows usage in this case:

> inspect clean
Task: Unit
Description:
    Deletes files produced by the build, such as generated sources, compiled classes, and task caches.
Dependencies:
    clean-files
    clean-keep-files

> inspect clean-files
Setting: scala.collection.Seq[java.io.File] = List(<project>/lib_managed, <project>/target, <project>/temp)
Description:
    The files to recursively delete during a clean.
Dependencies:
    managed-directory
    target
    base-directory

You can see this shows you if it is a task or setting, the type, a description, and the tasks/settings used as inputs.



回答2:

Mark Harrah's answer is now outdated.

Here is a version that works for sbt 0.13 and upwards, and is the only of the two versions that works in sbt 1.0 and onwards.

Updated key appending syntax.

Before sbt 0.13, you had the <+= syntax for adding additional values, and you could apply on keys.

With 0.13, a new, more uniform syntax was introduced, in part called value DSL. As of 1.0, the old syntax has been removed.

// From the old, apply-based version...
cleanFiles <+= baseDirectory { base => base / "temp" }

// ...we change to .value and collection-like syntax
cleanFiles += baseDirectory.value / "temp"

cleanFiles and other collection-based keys now mimic a (mutable) collection, so you can append values to it via a += operator. If you have multiple values, use ++= with a List or Seq instead.

.value does not force the evaluation of baseDirectory when you write it, but each time cleanFiles is calculated, so it's different for each sub-project.

Updated inspect and command syntax

There is also a slight difference in the inspect clean-files syntax.

  1. hyphen-named-commands have been deprecated in 0.13 and removed in 1.0. They were replaced with lowerCamelCaseCommands, so it's the same in the console as in your build.sbt.
  2. inspect doesn't show the value of your key anymore (I could not find a version information for that). Instead, you have to use show.

    sbt:root-_t> show cleanFiles
    [info] * C:\Users\Adowrath\_T\target\scala-2.12
    [info] * C:\Users\Adowrath\_T\target\streams
    [info] * C:\Users\Adowrath\_T\temp
    [success] Total time: 0 s, completed 06.02.2018, 10:28:00
    


回答3:

One possibility is that your tests clean up after themselves, like agilesteel noted.

Another possiblity is that you create a custom cleanup task which depends on the test task. See my answer here for more information on how to customize existing tasks (like test in your case): add dependency to existing rule.



回答4:

The previously suggested solution is deprecated now. Bellow is the code which works for me.

cleanFiles += new java.io.File(path)


标签: scala sbt