Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better to use task dependencies or task.doLast in Gradle?

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

like image 311
rfgamaral Avatar asked Jul 03 '13 01:07

rfgamaral


People also ask

What is doLast in Gradle task?

The doLast ensures that this code block is only executed during task execution. It basically tells Gradle during configuration to run this code block last while executing this task 1. We can also use the doFirst code block to run something first while executing the task.

Where should I add dependencies in Gradle project?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build. gradle file. This declares a dependency on an Android library module named "mylibrary" (this name must match the library name defined with an include: in your settings.

Does Gradle dependencies order matter?

The problem is that in eclipse, declaring a project dependency order determines both the source and jar order. (This is true whether or not the eclipse project exports its jars.) In Gradle, no matter what order we use in the dependencies declaration, project dependencies come before jar dependencies.

Which are the two types of plugins in Gradle?

There are two general types of plugins in Gradle, binary plugins and script plugins.


1 Answers

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.

like image 52
Peter Niederwieser Avatar answered Oct 24 '22 19:10

Peter Niederwieser