72

So far I've added the following to my build.gradle

apply plugin: 'base' 
clean << {
    delete '${rootDir}/api-library/auto-generated-classes/'
    println '${rootDir}/api-library/auto-generated-classes/'
}

However, not only is my file not deleted, but the print statement shows that ${rootDir} is not being converted to the root directory of my project.

Why won't this work, what concepts am I missing?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
oibe
  • 1,047
  • 1
  • 7
  • 10

7 Answers7

70

You just need to use double quotes. Also, drop the << and use doFirst instead if you are planning to do the deletion during execution. Something like this:

clean.doFirst {
    delete "${rootDir}/api-library/auto-generated-classes/"
    println "${rootDir}/api-library/auto-generated-classes/"
}

Gradle build scripts are written in Groovy DSL. In Groovy you need to use double quotes for string interpolation (when you are using ${} as placeholders). Take a look at here.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
mushfek0001
  • 3,845
  • 1
  • 21
  • 20
  • 1
    Not sure about it. It will be deleting the file at *configuration* phase, not during execution. – Opal Apr 23 '15 at 07:34
  • 5
    The above comment is not true. doFirst is only evaluated during the `execution` phase. _The calls doFirst and doLast can be executed multiple times. They add an action to the beginning or the end of the task’s actions list. When the task **executes**, the actions in the action list are executed in order._ [from docs](https://docs.gradle.org/current/userguide/tutorial_using_tasks.html) – kevinmm Jun 20 '18 at 21:29
62

<< is equivalent for clean.doLast. doFirst and doLast are ordering the operations at the execution phase, which is seldom relevant for delete operations.

In this case you don't need any of them. The clean task from base is of type Delete, so you simply need to pass it a closure to tell it at configuration time what to delete when it executes:

clean {
    delete 'someFile'
}

AS mushfek0001 correctly points it out in his answer, you should use double quotes for variable interpolation to work:

clean {
    delete "${buildDir}/someFile"
}

You need to have at least the base plugin applied for this to work, most other plugins, like the Java plugin either apply base or declare their own clean task of type delete Delete task. The error you would get if you don't have this is a missing clean method one.

apply plugin: 'base'
Alpar
  • 2,807
  • 23
  • 16
  • 2
    `delete '${buildDir}/someFile'` will not resolve the variable for `buildDir`. You need to use `"` here, e.g. `delete "$buildDir/someFile"` – Franz Becker Jul 21 '16 at 13:00
  • corrected the typo of `'` to `"`. Double wuotes are requried for [GStrings](http://docs.groovy-lang.org/latest/html/api/groovy/lang/GString.html) – Alpar Jul 24 '16 at 08:48
  • 1
    But this way you completely replace the default behavior of clean. What if he has some more tasks attached to it? – mushfek0001 Sep 05 '16 at 11:37
  • 5
    As pointed out in the documentation, the `delete` method of the `Delete` task: > Adds some files to be deleted by this task. As such, this does not replace previous behavior ( i.e. previous list of files to delete ), it just adds to it. – Alpar Sep 06 '16 at 05:44
  • This results in an error for me: `Could not find method clean() for arguments [build_6vh7849xb7rs3m0ite221ir66$_run_closure6@20f01807]` – Nate Glenn Jun 25 '17 at 10:51
  • if you want to delete many files use `delete fileTree(dir: "${projectDir}", include: '**.ext')` See https://stackoverflow.com/q/27285885/3495031 – forzagreen Nov 28 '17 at 13:23
19

In order to extend the clean task, you can use

clean.doFirst {}

or

clean.doLast {}

These will allow you to inject your own actions into the clean process. In order to delete files and directories you can use the "file" API which doesn't require any additional plugins.

Here is an example that will delete both a file and a directory as the last step in the clean task:

clean.doLast {
    file('src/main/someFile.txt').delete()
    file('src/main/libs').deleteDir()
}
bstar55
  • 3,542
  • 3
  • 20
  • 24
12

Gradle Kotlin Script analogue:

tasks {
    getByName<Delete>("clean") {
        delete.add("logs") // add accepts argument with Any type
    }
}
Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
12

Below one works for me (I prefer to use dependsOn),

task customCleanUp(type:Delete) {
   delete "your_folder", "your_file"
}

tasks.clean.dependsOn(tasks.customCleanUp)
Suryavel TR
  • 3,576
  • 1
  • 22
  • 25
  • It's important that the task is build with possibility of passing in parameters [Authoring Tasks](https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:up_to_date_checks). This way it's easy to register a new task of a particular type and then just define your own inputs. While if task reads gradle properties directly in the action itself and you don't have option to overwrite those, maybe you won't manage to copy/duplicate it and will have to rewrite the logic in TaskAction. – RenatoIvancic Oct 18 '22 at 14:41
1

There are options in gradle where you can extend a particular task ("clean" in your case) with doFirst (before the actual task), doLast (after the actual task).

You just need to replace single quotes with double quotes.

clean.doFirst {
    delete "${rootDir}/api-library/auto-generated-classes"
    println "${rootDir}/api-library/auto-generated-classes"
}
Valeria
  • 11
  • 1
1

Another solution for Kotlin DSL (build.gradle.kts):

tasks.clean {
    delete += listOf(
        "${rootDir}/logs.txt",
        projectDir.list()?.first { it/* name */ .contains("temp") }
    )
}

This deletes a file called logs.txt at the project root and deletes any directory/file that has temp in its name in this subproject (the folder containing this build file).

Mahozad
  • 18,032
  • 13
  • 118
  • 133