Gradle Goodness: Add Incremental Build Support to Tasks
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", version12.generateVersionFile.outputs.files outputFile13. 14.task showContents << {15.println outputFile.text16.}17.showContents.dependsOn generateVersionFileLet's run our script for the first time:
$ gradle showContents:generateVersionFile:showContentsVersion: 1.0BUILD SUCCESSFULNow we run it again and notice how Gradle tells us the task is UP-TO-DATE:
$ gradle showContents:generateVersionFile UP-TO-DATE:showContentsVersion: 1.0BUILD SUCCESSFULLet's change the build script and set the version to 1.1 and run Gradle:
$ gradle showContents:generateVersionFile:showContentsVersion: 1.1BUILD SUCCESSFULGradle Goodness: Add Incremental Build Support to Tasks的更多相关文章
- 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 ...
- 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 ...
- Gradle Goodness: Task Output Annotations Create Directory Automatically
Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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. ...
- Gradle Goodness: Copy Files with Filtering
Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...
随机推荐
- 一款基于HTML5的高性能WEBGIS介绍
远景地理信息系统(RemoteGIS)是一款基于HTML5的GIS平台软件,它使用Javascript开发,旨在解决当前WEBGIS矢量数据在数据量和刷新性能上的瓶颈,并利用WEB程序的跨平台特性,打 ...
- SSM 框架-02-MyEclipse 2017 安装与破解
SSM 框架-02-MyEclipse 2017 安装与破解 现在在学J2EE,然后使用的工具就是 MyEclipse,现在就抛弃 Eclipse 了,我就不多说它俩的区别了,但是 MyEclipse ...
- [转发]CPU个数、CPU核心数、CPU线程数
我们在选购电脑的时候,CPU是一个需要考虑到核心因素,因为它决定了电脑的性能等级.CPU从早期的单核,发展到现在的双核,多核.CPU除了核心数之外,还有线程数之说,下面文本就来解释一下CPU的核心数与 ...
- webbench 网站压力测试
[root@localhost ~]# webbench -c 500 -t 4 http://172.24.61.41/Webbench - Simple Web Benchmark 1.5Copy ...
- openresty及lua的随机函数
我们都知道,所谓的随机都是伪随机,随机的结果是由随机算法和随机种子决定的. 所以,当我们没有初始化的时候,如果直接使用math.random(),那么出来的值肯定是每次都一样,因为种子等于0. 因此, ...
- August 08th 2017 Week 32nd Tuesday
The very essence of romance is uncertainty. 浪漫的精髓就在于它充满种种可能. Romance is the glamour that can turn th ...
- December 06th 2016 Week 50th Tuesday
Behind every beautiful thing, there is some kind of pain. 美丽背后,必有努力. No pains, no gains. But it seem ...
- December 28th 2016 Week 53rd Wednesday
Knowledge is a treasure, but practice is the key to it. 知识是珍宝,而实践是获取她的钥匙. I know a lot, but what I r ...
- 在python中逐行读取大文件
在我们日常工作中,难免会有处理日志文件的时候,当文件小的时候,基本不用当心什么,直接用file.read()或readlines()就可以了,但是如果是将一个10G大小的日志文件读取,即文件大于内存的 ...
- 3、NPM使用
内容:NPM介绍,安装web框架模块,一些基本命令 #####介绍NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: • 允许用户从 ...