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. C#与.NET的区别和C#程序结构

    C#语言及其特点 (1)语法简洁,不允许直接操作做内存,去掉指针操作 (2)彻底的面向对象设计,C#具有面向对象所应用的一切特性:封装.继承.多态 (3)与Web紧密结合,C#支持绝大多数的Web标准 ...

  2. css常见的快捷开发代码汇总(长期更新)

    http://caibaojian.com/popular-css-snippets.html

  3. JavaWeb学习总结(十一):Session解决form表单重复提交

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...

  4. 在IE、fixfox、chrome等浏览器中ajax提交成功后,打开新标签页面被浏览器拦截问题[转]

    如题: 在项目中要在当前页面中,再新开一个页面, 新开页面的地址是ajax请求后返回的url --------- 试了,浏览器提示组织弹窗..... 网上找,找到了一个处理方式,思路是 1. 先打开一 ...

  5. IOS xcode安装

    xcode软件下载地址: 可以通过虚拟机共享文件夹将xcode传递给虚拟机上的os系统: 第一个程序创建:

  6. js 正则常用函数

    正则表达式中,需要转义的字符: * . ? + $ ^ [ ] ( ) { } | \ / let reg = /\d+/g let str = 'ad/23/dfww/454/6' 1. reg.t ...

  7. v-model实现

    v-model就是输入的值实时显示的目的,如果纯粹写登录页面的form控件没有实时显示的需求 就不用绑定v-model.

  8. winform基础控件-例子学习

    1.如图实现整数计算器 ComboBox控件: Items属性:添加集合中的项. this.comoper.Items.AddRange(new object[] { "+", & ...

  9. NodeJS的特点

    一. NodeJS的特点 我们先来看看NodeJS官网上的介绍: Node.js is a platform built on Chrome’s JavaScript runtime for easi ...

  10. [翻译] KVNProgress

    KVNProgress KVNProgress is a fully customizable progress HUD that can be full screen or not. KVNProg ...