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

前端 未结 5 828
傲寒
傲寒 2021-02-01 00:29

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

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


        
5条回答
  •  长发绾君心
    2021-02-01 00:58

    << 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'
    

提交回复
热议问题