12

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... :)

rfgamaral
  • 16,546
  • 57
  • 163
  • 275

1 Answers1

14

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.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 1
    How do you recommend to create those tasks dependencies? I would like a single task to assemble the whole project (basically "replacing" `assembleRelease`) and both of those tasks must be executed only **after a successful** execution of `assembleRelease`. Between them, the order is not really relevant. – rfgamaral Jul 03 '13 at 08:41
  • 6
    `myBuildTask.dependsOn(updateVersionProperties, archiveOutputFile); updateVersionProperties.dependsOn(assembleRelease); archiveOutputFile.dependsOn(assembleRelease)` – Peter Niederwieser Jul 03 '13 at 17:58