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"
, 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的更多相关文章
- 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 ...
随机推荐
- 自定义scoll样式
使用伪类自定义scroll样式 效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...
- C#与.NET的区别和C#程序结构
C#语言及其特点 (1)语法简洁,不允许直接操作做内存,去掉指针操作 (2)彻底的面向对象设计,C#具有面向对象所应用的一切特性:封装.继承.多态 (3)与Web紧密结合,C#支持绝大多数的Web标准 ...
- js 控制选中文字
//脚本获取网页中选中文字 var word = document.selection.createRange().text; //获取选中文字所在的句子 var range = documen ...
- bootstrap 中的 iCheck 全选反选功能的实现
喜欢bootstrap 风格的同学应该知道,iCheck的样式还是很好看的. 官网: http://www.bootcss.com/p/icheck/ 进入正题,iCheck提供了一些方法,可以进行全 ...
- LeetCode赛题----Find Left Most Element
问题描述 Given a binary tree, find the left most element in the last row of the tree. Example 1: Input: ...
- 整理一下最近Android面试的提问
java相关: 1. public protect private default关键字有什么区别? public:表示可以在任何一个类中被访问: protect:表示可以在自身.子类以及同一包下的类 ...
- How do I use the API correctly
1:打开帮助文档2:点击显示,找到索引,看到输入框3:你要学习什么内容,你就在框框里面输入什么内容 举例:Random4:看包 java.lang包下的类在使用的时候是不需要导包的5:看类的描述 Ra ...
- 关于serialVersionUID与序列化"
java序列化trick and trap 厂内经常出现序列化对象版本不匹配问题,于是发本文说明一些序列化的注意点 调用MQ.memcached.rpc等等涉及到远程通讯的都会经过序列化,虽然客户端透 ...
- 从golang-gin-realworld-example-app项目学写httpapi (一)
https://wangzitian0.github.io/2013/06/29/zero-to-one-1/ https://github.com/gothinkster/golang-gin-re ...
- django项目配置
创建工程 本项目使用git管理项目代码,代码库放在gitee码云平台.(注意,公司中通常放在gitlab私有服务器中) 1. 在git平台创建工程 1) 创建私有项目库 2)克隆项目到本地 3)创建并 ...