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. Centos的一个find命令配合rm删除某天前的文件

    语句写法:find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \; 例1: 将/usr/local/backups目录下所有10天前带 ...

  2. python中logging日志基本用法,和进程安全问题

    低配版 import logging logging.debug('debug message') # 调试模式 logging.info('info message') # 正常运转模式 loggi ...

  3. css实现中间文字,两边横线效果

    1. vertical-align属性实现效果: vertical-align 属性设置元素的垂直对齐方式. 该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐.允许指定负长度值和百分比值. ...

  4. html中的行内元素和块级元素小结

    一.首先我们总结下行内元素和块级元素有哪些: 行内元素: <a>标签可定义锚<abbr>表示一个缩写形式<acronym>定义只取首字母缩写<b>字体加 ...

  5. ArcGISPlotSilverlightAPI For WPF

    这两天有个需求,在地图上做标绘箭头,效果如下图. Arcgis for WPF 10.2.5.0版本,然而官方文档中没有这种API,自己去写一个呢,又感觉无从下手.无奈去网上搜索了一下,发现一篇好文: ...

  6. ASP.NET MVC学习笔记 第二天

    创建视图      返回给客户端的HTML代码最好通过视图指定.视图都在Views文件夹中定义.ViewsDemo控制器的视图需要一个ViewsDemo子目录,这是视图的约定.      可以把多个控 ...

  7. Node.js 常用 API

    Node.js v6.11.2  Documentation(官方文档) Buffer Prior to the introduction of TypedArray in ECMAScript 20 ...

  8. php 3des加密 兼容JAVA 多么痛的领悟呀

    最近和别人做接口用到SOCKET TCP/IP方式 其中需要对账号和密码进行3DES加密 对方提供了一个加密比对的软件和JAVA的实现代码 并且给了我们一个长度为32位的密钥 这边需要用PHP来实现! ...

  9. Linux系统环境下安装dedecms(织梦)提示http500错误的解决办法

    碰到一客户安装DEDE提示http500错误,问题已得到完美解决,下面我分享下 这个解决办法,希望有帮助. 故障状态:正常安装dedecms v5.7 gbk提示http500错误Dede安装环境:一 ...

  10. Recursive functions and algorithms

    http://en.wikipedia.org/wiki/Recursion_(computer_science)#Recursive_functions_and_algorithms A commo ...