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 bind()与delegate()的区别

    笔试题: bind()与delegate()的区别主要有三点: 1 绑定目标 .bind直接绑在目标元素上 .delegate绑在父元素上 2  监听个数 .bind监听个数多——每个目标元素都需要添 ...

  2. 第九天- 文件操作 r w a 文件复制/修改

    文件操作简介:使用python来读写文件是非常简单的操作.我们使用 open() 函数来打开一个文件,获取到文件句柄.然后通过文件句柄就可以进行各种各样的操作了.根据打开⽅方式的不同能够执行的操作也会 ...

  3. android 获取http请求json数据

    package com.my.gethttpjsondata; import java.io.BufferedReader;import java.io.ByteArrayOutputStream;i ...

  4. 在PHP中使用加密技术

    Gpg4win 是一款基于 GPG 的非对称加密软件.非对称加密方式,简单理解就是用公钥加密文件,用私钥解密文件.如果你需要发送加密信息,首先获取接收者的公钥,然后利用该公钥加密后传递,对方利用对应的 ...

  5. Spring mvc与Struts2的比较

    1. 机制:spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. 2. 性能:spring会稍微比struts快.spring mvc是基于方法的设 ...

  6. Android dialog圆角显示及解决出现的黑色棱角

    最近在开发一个天气预报的app,看到一个比较不错友情提示,如下:                怎么样,看起来比原始的dialog好看吧.好了,做法也许有很多,我介绍下我的做法吧, 首先,我第一个想到 ...

  7. CSS中的line-height

    基本概念 行高.行距 行高是指文本行基线间的垂直距离.那什么是基线呢?记不记得vertical-align属性有个baseline值,这个baseline就是基线. 注意:倒数第二根才是基线(base ...

  8. String类型的学习

    一 :关于两个string类型变量是否相等: 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 分析: 首先为s0开辟空间,然后给s1开辟 ...

  9. win8.1 安装msi软件出现 2503、2502

    问题现象: 安装Msi封包的程序的时候,老是提示 2503 和 2502 错误. 解决办法: 命令提示符提示安装程序权限 右击开始按钮,然后选择命令提示如(管理员)

  10. 【Leetcode】【hard】Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...