Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extend gradle's clean task to delete a file?

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?

like image 222
oibe Avatar asked Apr 23 '15 03:04

oibe


People also ask

How do I delete a file in Gradle?

Use the Gradle Delete task. @GuerinoRodella no need, there is already a project. delete() method for this purpose.

What does Gradlew clean do?

it removes build folder, as well configure your modules and then build your project. i use it before release any new app on playstore.

What is FileTree in Gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.


2 Answers

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.

like image 191
mushfek0001 Avatar answered Sep 20 '22 23:09

mushfek0001


<< 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' 
like image 20
Alpar Avatar answered Sep 18 '22 23:09

Alpar