zoukankan      html  css  js  c++  java
  • Gradle Goodness: Add Incremental Build Support to Custom Tasks with Annotations

    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
  • 相关阅读:
    c#redis使用
    不安全的HTTP方法(渗透实验)
    arguments.callee弃用与webuploader
    多线程系列1:经典卖票
    终于确定了系统lsass.exe占用cpu的根本原因了,速度来看一看!![转载]
    edit响应键盘的“咚咚”声音去掉
    delphi资源文件制作及使用详解
    MySQL server has gone away错误的解决办法
    MySQL server has gone away的解决方法
    MySQL
  • 原文地址:https://www.cnblogs.com/GoAhead/p/4189150.html
Copyright © 2011-2022 走看看