Add an additional directory to clear the task in SBT build

I have an SBT assembly where tests create temporary files in a directory called temp. How can I tell SBT to delete this folder when calling a clean task?

+6
source share
4 answers

Use this parameter in a project containing the temp directory:

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

This adds the temp directory to the list of files that will be recursively deleted when clean .

< means "configure in terms of other tasks / settings", + means adding files to the current list, and baseDirectory is a parameter that provides the project’s base directory.

You can see how clean configured using the inspect command, described in more detail on the Inspecting Settings page. The 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 that this shows whether it is a task or parameter, type, description and tasks / parameters used as inputs.

+12
source

One possibility is that your tests are cleaned up after themselves, as agilesteel noted.

Another possibility is to create a custom cleaning task, which depends on the test task. See My answer here for more information on how to configure existing tasks (for example, a test in your case): add dependency on an existing rule .

+1
source

The previously proposed solution is out of date. Bellow is code that works for me.

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

Mark answer Harrah is now deprecated.

Here is the version that works for sbt 0.13 and higher and is the only one of the two versions that work in sbt 1.0 onwards.

Updated key addition .

Prior to sbt 0.13, you had the <+= syntax for assigning values, and you could apply on the keys.

From 0.13 a new, more uniform syntax was introduced, partly called value DSL. Starting with version 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 the (mutable) collection, so you can add values ​​to it using the += operator.

.value does not force the baseDirectory to be evaluated when it is written, but cleanFiles evaluated each time, so it is different for each subproject.

Check and command syntax updated

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

  • hyphen-named-commands are deprecated at 0.13 and removed at 1.0. They have been replaced with lowerCamelCaseCommands , so they are the same on the console as in your build.sbt .
  • inspect no longer shows the value of your key (I could not find version information for this). Instead, you should 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 
0
source

Source: https://habr.com/ru/post/915019/


All Articles