In a previous post we learned how we can use the inputs and outputs properties to set properties or files that need to be checked to see if a task is up to date. In this post we learn how a custom task class can use annotations to set input properties, file or files and output files or dir.

For input we can use @Input, @InputFile, @InputFiles or @InputDirectory annotations. Gradle uses the properties with annotations for checking if a task is up to date. Output file or directory can be marked with @OutputFile and @OutputDirectory.

00.task generateVersionFile(type: Generate) {
01.version = '2.0'
02.outputFile = file("$project.buildDir/version.txt")
03.}
04. 
05.task showContents << {
06.println generateVersionFile.outputFile.text
07.}
08.showContents.dependsOn generateVersionFile
09. 
10.class Generate extends DefaultTask {
11.@Input
12.String version
13. 
14.@OutputFile
15.File outputFile
16. 
17.@TaskAction
18.void generate() {
19.def file = getOutputFile()
20.if (!file.isFile()) {
21.file.parentFile.mkdirs()
22.file.createNewFile()
23.}
24.file.write "Version: ${getVersion()}"
25.}
26.}

We can run our task and get the following output:

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

And if we run it again we see the task is now up to date:

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

We can change the version numer in our build script to 2.1 and see the output:

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

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

  1. Gradle Goodness: Add Incremental Build Support to Tasks

    Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unle ...

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

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

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

  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. python可变容器类型做函数参数的坑

    def extendList(val, list=[]): # []默认参数的只指向一个地址 list.append(val) return list list1 = extendList(10) l ...

  2. C# Time Class using MySQL

    http://www.csharphelp.com/2007/09/c-time-class/ /* datatypes. Time class is writen in C# and .NET 2. ...

  3. 精选10款HTML5手机模板

    1.Stroller | Mobile & Tablet Responsive Template 演示地址   购买地址 2.Ocean Mobile Template 演示地址   购买地址 ...

  4. bootstrap学习笔记(网页开发小知识)

    这是我在学习Boostrap网页开发时遇到的主要知识点: 1.导航条navbar 添加.navbar-fixed-top类可以让导航条固定在顶部,固定的导航条会遮住页面上的其他内容,除非给<bo ...

  5. linq返回的IEnumerable<T>泛型不能被列举计算大于1次

    在分页获取Take后面加ToList()方法就能得到正确结果,为什么?

  6. FineReport移动端如何获取地址位置

    对于企业大多数员工来说,由于其工作位置是固定的,可以有多种方式进行上班打卡签到以保证该员工有按时正常来上班,但是对于经常需要出差,去客户现场的员工来说,就无法保证他们是否有去上班,所以希望能通过手机位 ...

  7. Angular1.x 基础总结

    官方文档:Guide to AngularJS Documentation   w3shools    angularjs教程  wiki   <AngularJS权威教程> Introd ...

  8. 一次线上bug引起的反思

    今天线上又出现了一个bug,而且代码是我写的.之前这个问题也出现过,不过由于每次情况都不同,改来改去总是改不完.之后领导知道后也很恼火,让测试把每种情况都测试了下,而我也又一次重新检查了下代码.当时确 ...

  9. 个人总结-9-session的使用,十天免登陆

    昨天查看bootstrap,实现了登录和注册页面的重写. 今天准备加入session实现,十天免登陆等内容. 使用bootstrap直接套用标签页,以实现.

  10. java基础问题解答

    Java学习中的问题   一 枚举类型: 下面是一段源程序代码: package Demo; public class Enum { public static void main(String[] ...