zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    5个最好用AngularJS构建应用程序框架
    5款最好的免费在线网站CSS验证器
    10款最优秀的开源移动开发工具
    10个最好的免费PS图象处理软件方案
    10个基本的HTML5动画工具设计
    6款最好的免费在线二维码生成器
    Redis配置文件参数说明
    Redis学习手册(主从复制)
    java.lang.OutOfMemoryError: PermGen space PermGen space & java.lang.OutOfMemoryError: Java heap space Heap siz
    TNSNAMES.ORA 配置
  • 原文地址:https://www.cnblogs.com/GoAhead/p/4189157.html
Copyright © 2011-2022 走看看