Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This task is able to remove any output files or directories we have defined for our task. For example we can assign an output file or directory to our task with the outputs property. Or we can use the @OutputFile and @OutputDirectories annotations for custom task classes. The clean<Taskname> rule can delete the output files or directories for the task with the name <Taskname> for us. We don't have to write the clean task ourselves we only have define the base plugin in our project. And Gradle will take care of the rest!.

00.apply plugin: 'base'
01. 
02.outputDir = file("$buildDir/generated-src")
03.outputFile = file("$buildDir/output.txt")
04. 
05.task generate << {
06.outputDir.mkdirs()
07.outputFile.write 'Generated by Gradle.'
08.}
09.generate.outputs.files outputFile
10.generate.outputs.dir outputDir
11. 
12.task showBuildDir << {
13.def files = buildDir.listFiles()
14.files.each {
15.print   it.directory ? 'Dir:  ' : 'File: '
16.println it.name
17.}
18.println "${files.size()} files in $buildDir.name"
19.}

We can first run the generate task and see the output file and directory.

$ gradle -q generate showBuildDir
Dir:  generated-src
File: output.txt
2 files in build

Next we can run the task cleanGenerate, which is added to the project by Gradle, and see the output files are gone.

$ gradle -q cleanGenerate showBuildDir
0 files in build

Gradle Goodness: Automatic Clean Tasks的更多相关文章

  1. Gradle Goodness: Display Available Tasks

    To see which tasks are available for our build we can run Gradle with the command-line option -t or ...

  2. Gradle Goodness: Group Similar Tasks

    In Gradle we can assign a task to a group. Gradle uses the group for example in the output of $ grad ...

  3. Gradle Goodness: Continue Build Even with Failed Tasks

    If we run a Gradle build and one of the tasks fails, the whole build stops immediately. So we have f ...

  4. Gradle Goodness: Task Output Annotations Create Directory Automatically

    Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of ...

  5. Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

    Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. ...

  6. Gradle Goodness: Copy Files with Filtering

    Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...

  7. Gradle Goodness: Changing Name of Default Build File

    Gradle uses the name build.gradle as the default name for a build file. If we write our build code i ...

  8. Gradle Goodness: Adding Tasks to a Predefined Group

    In Gradle we can group related tasks using the group property of a task. We provide the name of our ...

  9. Gradle Goodness: Excluding Tasks for Execution

    In Gradle we can create dependencies between tasks. But we can also exclude certain tasks from those ...

随机推荐

  1. git杂记-打标签

    列出标签 $ git tag v0. v1. 创建标签 --添加附注标签(推荐):加上-a选项(annotated) $ git tag -a v1. -m 'my version 1.4' --添加 ...

  2. MySQL mysqldump数据导出基本操作

    mysqldump mysqldump命令是mysql数据库中备份工具,用于将MySQL服务器中的数据库以标准的sql语言的方式导出,并保存到文件中. 选项 --all-databases, -A:导 ...

  3. 网络测速 php代码

    <?php /*=====http://hi.csdn.net/yinyiniao=====*/ $fp=fopen("cs.txt","w"); for ...

  4. python 反射 动态导入模块 类attr属性

    1.反射 hasattr getattr delattr setattr 优点:事先定义好接口,接口只有在被完成后才能真正执行,这实现了即插即用,这其实是一种“后期绑定”,即先定义好接口, 然后是再去 ...

  5. vue之computed(计算属性)

    所谓计算属性就是计算data里的数据属性. computed:实时监听的该功能. 即监听是否有修改(浏览器未打开时即开始监听了),监听的值有修改则添加 所监听的data数据属性变化了,自动实时修改. ...

  6. 如何查看服务器CPU核心数和线程数

    知道服务器CPU型号,那么我们如何在服务器里面查看服务器CPU核心数和线程数呢? 步骤: 先用鼠标右键点击屏幕最下方的任务栏空白处.会弹出一个菜单. 在菜单中用鼠标左键点选“启动任务管理器”. 点击任 ...

  7. java笔记--代码实现汉诺塔移动过程和移动次数

    汉诺塔 有三根相邻的柱子,标号为A,B,C,A柱子上从下到上按金字塔状叠放着n个不同大小的圆盘,要把所有盘子一个一个移动到柱子B上,并且每次移动同一根柱子上都不能出现大盘子在小盘子上方. --如果朋友 ...

  8. Linux工具-nmon

    1.nmon下载地址:https://sourceforge.net/projects/nmon/files/ 创建文件nmon:# mkdir nmon 解压文件夹:# tar -zxvf nmon ...

  9. Ubuntu apt-get 更换源

    Ubuntu apt-get 更换源 我们使用清华的镜像源进行更换 Ubuntu 的软件源配置文件是 /etc/apt/sources.list.将系统自带的该文件做个备份,将该文件替换为下面内容,即 ...

  10. codeforces 633E Startup Funding(浮点数处理)

    codeforces 633E Startup Funding 题意 枚举左端点,对于每个左端点求一个最大的右端点使得最大. 对于得到的这个数组,随机选择k个数,求最小值期望. 题解 对于每个左端点, ...