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. idea新建maven多模块spring boot项目

    1.新建一个maven多模块项目,比如这种结构: maven-demo |--demo-common |--demo-order |--demo-user 2.先新建一个maven项目,在maven项 ...

  2. Chrome控制台毫无反应,打印不出信息了?

    最近在使用console.log()方法的时候遇到一个奇怪的问题,打开chrome控制台想调试代码,结果控制台半天无反应,让我纳闷了半天.详情如图所示: 然后我又打开了新的标签页,不行!接着干脆关闭浏 ...

  3. 汇编语言程序环境搭建masm+debug64位 win10/7

    介绍:MASM是Microsoft Macro Assembler 的缩写,是微软公司为x86 微处理器家族开发的汇编开发环境,拥有可视化的开发界面,使开发人员不必再使用DOS环境进行汇编的开发,编译 ...

  4. 【分享】BS大神的C++ 11 keynotes

    看到infoQ上面有BS大神的keynotes讲C++ 11的,有点长,但是值得一看.   http://www.infoq.com/presentations/Cplusplus-11-Bjarne ...

  5. 【Yii系列】Yii2.0的安装与调试

    接上一节的话,我们最终选择了Yii框架作为我们的主要开发框架,今天,我就和大伙来聊聊如何安装与调试Yii2.0,以及后续会和大伙聊聊如何在Yii2.0上快速撸代码. Yii2.0的安装 好的,Comp ...

  6. 微信小程序 - 弹出键盘遮挡住输入框

    在开发微信小程序的时候遇到,输入用户名或者手机号以及地址,手机键盘调起来,会把输入框遮挡. 如图: 以上两张图是自己工作中遇到的,此处不要着急,一个属性帮你搞定. cursor-spacing:指定光 ...

  7. SqlServer 批量添加记录

    declare @i int ) begin INSERT INTO [dbo].[Settlements] ([ID] ,[Count] ,[SettlementDate]) VALUES ( ne ...

  8. 比较和排序(IComparable和IComparer以及它们的泛型实现)(转)

    C#笔记25:比较和排序(IComparable和IComparer以及它们的泛型实现) 本文摘要: 1:比较和排序的概念: 2:IComparable和IComparer: 3:IComparabl ...

  9. winform ComboBox控件反选

    winform ComboBox控件反选:int index = comboBox1.FindString(textBox2.Text); comboBox1.SelectedIndex = inde ...

  10. MySQL 数据库--内置功能

    一 视图 视图:是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 优点:们可以把查询过程中的临 ...