Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unless it is necessary. We can help Gradle and configure our task so it is ready for an incremental build.

Suppose we have a task that generates a file. The file only needs to be generated if a certain property value has changed since the last task execution. Or the file needs be generated again if a source file is newer than the generated file. These conditions can be configured by us, so Gradle can use this to determine if a task is up to date or not. If the task is up to date Gradle doesn't execute the actions.

A Gradle task has an inputs and outputs property. We can assign a file(s), dir or properties as inputs to be checked. For outputs we can assign a file, dir or custom code in a closure to determine the output of the task. Gradle uses these values to determine if a task needs to be executed.

In the following sample build script we have a task generateVersionFile which create a file version.text in the project build directory. The contents of the file is the value of the version property. The file only needs to be generated if the value of version has changed since the last time the file was generated.

00.version = '1.0'
01.outputFile = file("$buildDir/version.txt")
02. 
03.task generateVersionFile << {
04.if (!outputFile.isFile()) {
05.outputFile.parentFile.mkdirs()
06.outputFile.createNewFile()
07.}
08.outputFile.write "Version: $version"
09.}
10. 
11.generateVersionFile.inputs.property "version", version
12.generateVersionFile.outputs.files outputFile
13. 
14.task showContents << {
15.println outputFile.text
16.}
17.showContents.dependsOn generateVersionFile

Let's run our script for the first time:

$ gradle showContents
:generateVersionFile
:showContents
Version: 1.0
 
BUILD SUCCESSFUL

Now we run it again and notice how Gradle tells us the task is UP-TO-DATE:

$ gradle showContents
:generateVersionFile UP-TO-DATE
:showContents
Version: 1.0
 
BUILD SUCCESSFUL

Let's change the build script and set the version to 1.1 and run Gradle:

$ gradle showContents
:generateVersionFile
:showContents
Version: 1.1
 
BUILD SUCCESSFUL

Gradle Goodness: Add Incremental Build Support to Tasks的更多相关文章

  1. Gradle Goodness: Add Incremental Build Support to Custom Tasks with Annotations

    In a previous post we learned how we can use the inputs and outputs properties to set properties or ...

  2. Gradle Goodness: Run a Build Script With a Different Name

    Normally Gradle looks for a build script file with the name build.gradle in the current directory to ...

  3. Gradle Goodness: Task Output Annotations Create Directory Automatically

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

  4. Gradle Goodness: Unpacking an Archive

    To create an archive with Gradle is easy. We have several tasks like Zip, Tar, Jar, War and Ear to c ...

  5. 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 ...

  6. 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 ...

  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: Init Script for Adding Extra Plugins to Existing Projects

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

  9. Gradle Goodness: Copy Files with Filtering

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

随机推荐

  1. MySQL之多表查询练习

    一.表格 表一  emp 表二 dept 表三 salgrade; 表四 年度利润表 二.习题 1. 查出至少有一个员工的部门.显示部门编号.部门名称.部门位置.部门人数. 2. 列出所有员工的姓名及 ...

  2. sql:SQL Server metadata queries

    http://www.mssqltips.com/sqlservertip/3449/making-sql-server-metadata-queries-easier-with-these-new- ...

  3. Uncaught TypeError: timeout.close is not a function. when try to use clearInterval

    It's because of your IDE! Make sure you have added automatic imports such as import { clearInterval ...

  4. C语言——二叉排序树

    二叉排序树是一种实现动态查找的树表,又称二叉查找树. 二叉排序树的性质: 1. 若它的左子树不为空,则左子树上所有节点的键值均小于它的根节点键值 2. 若它的右子树不为空,则右子树上所有节点的键值均大 ...

  5. How to block a specific IP Address using UFW

    How to block a specific IP Address using UFW The key to blocking a specific IP address with UFW is t ...

  6. apk 反编译 - 最新版图文教程

    apk 反编译 - 最新版图文教程 结合网上众多教程,整理一篇自己操作的,工具都是目前最新版 apk 反编译也就是将打包后的 apk 反编译为资源文件(图片).layout.样式.相关的实现代码等.( ...

  7. Android适配--百分比的适配

    首先,需要添加com.android.support:percent:24.1.1 包,版本随意. dependencies { compile fileTree(dir: 'libs', inclu ...

  8. C语言中关键词static的用法与作用域

    一.面向过程设计中的static 转载:http://blog.csdn.net/celerylxq/article/details/6160499 1.静态全局变量 在全局变量前,加上关键字stat ...

  9. RAP, 高效前后端联调框架,接口文档管理工具

    RAP通过GUI工具帮助WEB工程师更高效的管理接口文档,同时通过分析接口结构自动生成Mock数据.校验真实接口的正确性,使接口文档成为开发流程中的强依赖.有了结构化的API数据,RAP可以做的更多, ...

  10. Python学习---抽屉框架分析[数据库设计分析]180313

    基本的: models.py ####################################以下都是抽屉的代码#################################### fro ...