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. jquery 提示语淡入效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Bzoj4044 Virus synthesis

    题意 你要用 \(ATGC\) 四个字母用两种操作拼出给定的串: 将其中一个字符放在已有串开头或者结尾 将已有串复制,然后 \(reverse\) ,再接在已有串的头部或者尾部 一开始已有串为空.求最 ...

  3. C# Timer定时器用法

    System.Timers.Timer timer1 = new System.Timers.Timer(); timer1.Elapsed += new System.Timers.ElapsedE ...

  4. Vue 框架-01- 入门篇 图文教程

    Vue 框架-01- 入门篇 图文教程 Vue 官网:https://cn.vuejs.org/ 关于 Vue 的基础大家可以在官网的[起步]去学习,本系列文章主要针对实例项目应用 一.Vue 的安装 ...

  5. Android MediaPlayer 和 MediaCodec 的区别和联系(一)

    目录: (1)概念解释 : 硬解.软解 (2)Intel关于Android MediaCodec的相关说明 正文: 一.硬解.软解         (1)概念:                 a.硬 ...

  6. 在notepad++中快速插入当前时间方法

    插件是notepad++的一大优势,而要实现此功能,也必须借助TextFX插件. 1.点击"插件"-->"Plugin Manager"-->&qu ...

  7. Python学习---Python安装与基础1205

    1.0. 安装 1.1.1. 下载 官网下载地址:https://www.python.org/downloads/release/python-352/ 1.1.2. 配置环境变量 因为在安装的时候 ...

  8. 数据链路层 点对点协议 PPP

    点对点协议 PPP 一. PPP 协议应满足的需求 简单.提供不可靠的数据报服务,比IP协议简单,不需要纠错,不需要序号,不需要流量控制. 工作方式:接收方每收到一个帧就进行CRC校验,如正确就接受该 ...

  9. xsd文件记录

    <MESSAGE Version="1.0"> <CV_HEADER MsgType=" /> <QUERY_PROFILE> < ...

  10. December 17th 2016 Week 51st Saturday

    Great minds have purpose, others only have wishes. 杰出的人有着目标,其他人只拥有愿望. Are you clear about the differ ...