• 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
  • 相关阅读:
    循环语句
    流程控制
    特殊的赋值运算符
    位运算符
    运算符
    八种基本类型
    cmd基础命令
    springboot项目部署到tomcat步骤以及常见问题
    【算法问题】如何实现大整数相加
    【算法问题】删除k个数字后的最小值
  • 原文地址:https://www.cnblogs.com/GoAhead/p/4189157.html
Copyright © 2020-2023  润新知