更好地在Gradle中使用任务依赖关系或task.doLast? [英] Better to use task dependencies or task.doLast in Gradle?

查看:2495
本文介绍了更好地在Gradle中使用任务依赖关系或task.doLast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用Gradle构建我的最终输出文件之后,我想做两件事。更新本地version.properties文件并将最终输出最终复制到某个特定目录进行归档。假设我已经实现了2个方法,它们完全符合我刚刚描述的 updateVersionProperties()和 archiveOutputFile()



我知道想知道做这件事的最好方法是什么......



方案A: (b)

pre $ assembleRelease.doLast {
updateVersionProperties()
archiveOutputFile()
}

方案B:

 任务myBuildTask(dependsOn:assembleRelease)<< {
updateVersionProperties()
archiveOutputFile()
}

在这里我会打电话给 myBuildTask ,而不是像 assembleRelease 那样。

推荐使用哪种方式这和为什么?相对于另一个有什么优势吗?希望澄清一下...)

解决方案

只要您可以,将新活动建模为单独的任务。 (在你的情况下,你可能会增加两个任务。)这有很多好处:


  • 能够声明任务输入和输出(获得来自此的所有好处)
  • 能够重复使用现有的任务类型
  • Gradle并行执行任务的更多可能性

  • 等等



  • 有时,将活动建模为单独的任务并不容易。 (例如,有必要在现场对现有任务的输出进行后处理。在单独的任务中执行此操作会导致原始任务永远不会是最新的在后续运行中)。只有这样,才能将活动附加到具有 doLast 的现有任务中。


    After building my final output file with Gradle I want to do 2 things. Update a local version.properties file and copy the final output final to some specific directory for archiving. Let's assume I already have 2 methods implemented that do exactly what I just described, updateVersionProperties() and archiveOutputFile().

    I'm know wondering what's the best way to do this...

    Alternative A:

    assembleRelease.doLast {
        updateVersionProperties()
        archiveOutputFile()
    }
    

    Alternative B:

    task myBuildTask(dependsOn: assembleRelease) << {
        updateVersionProperties()
        archiveOutputFile()
    }
    

    And here I would call myBuildTask instead of assembleRelease as in alternative A.

    Which one is the recommended way of doing this and why? Is there any advantage of one over the other? Would like some clarification please... :)

    解决方案

    Whenever you can, model new activities as separate tasks. (In your case, you might add two more tasks.) This has many advantages:

    • Better feedback as to which activity is currently executing or failed
    • Ability to declare task inputs and outputs (reaping all benefits that come from this)
    • Ability to reuse existing task types
    • More possibilities for Gradle to execute tasks in parallel
    • Etc.

    Sometimes it isn't easily possible to model an activity as a separate task. (One example is when it's necessary to post-process the outputs of an existing task in-place. Doing this in a separate task would result in the original task never being up-to-date on subsequent runs.) Only then the activity should be attached to an existing task with doLast.

    这篇关于更好地在Gradle中使用任务依赖关系或task.doLast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆