• Gradle Goodness: Add Incremental Build Support to Tasks


    Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unless it is necessary. We can help Gradle and configure our task so it is ready for an incremental build.

    Suppose we have a task that generates a file. The file only needs to be generated if a certain property value has changed since the last task execution. Or the file needs be generated again if a source file is newer than the generated file. These conditions can be configured by us, so Gradle can use this to determine if a task is up to date or not. If the task is up to date Gradle doesn't execute the actions.

    A Gradle task has an inputs and outputs property. We can assign a file(s), dir or properties as inputs to be checked. For outputs we can assign a file, dir or custom code in a closure to determine the output of the task. Gradle uses these values to determine if a task needs to be executed.

    In the following sample build script we have a task generateVersionFile which create a file version.text in the project build directory. The contents of the file is the value of the version property. The file only needs to be generated if the value of version has changed since the last time the file was generated.

    00.version = '1.0'
    01.outputFile = file("$buildDir/version.txt")
    02. 
    03.task generateVersionFile << {
    04.if (!outputFile.isFile()) {
    05.outputFile.parentFile.mkdirs()
    06.outputFile.createNewFile()
    07.}
    08.outputFile.write "Version: $version"
    09.}
    10. 
    11.generateVersionFile.inputs.property "version", version
    12.generateVersionFile.outputs.files outputFile
    13. 
    14.task showContents << {
    15.println outputFile.text
    16.}
    17.showContents.dependsOn generateVersionFile

    Let's run our script for the first time:

    $ gradle showContents
    :generateVersionFile
    :showContents
    Version: 1.0
     
    BUILD SUCCESSFUL

    Now we run it again and notice how Gradle tells us the task is UP-TO-DATE:

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

    Let's change the build script and set the version to 1.1 and run Gradle:

    $ gradle showContents
    :generateVersionFile
    :showContents
    Version: 1.1
     
    BUILD SUCCESSFUL
  • 相关阅读:
    HDU
    C# Stopwatch
    RMQ(Range Minimum Query)问题(转)
    HDU
    POJ
    HDU
    POJ
    POJ
    docker安装testlink
    廖雪峰Java2面向对象编程-3继承和多态-2多态
  • 原文地址:https://www.cnblogs.com/GoAhead/p/4189157.html
Copyright © 2020-2023  润新知