Gradle Goodness:add Incremental Build support to Custom Tasks with Annotations

Source: Internet
Author: User

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

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

View Sourceprint? 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 that the task is now a to date:

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

We can change the version of 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.