Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do something before Gradle task's dependencies are executed?

Tags:

gradle

I have the following in build.gradle:

task aoeu << {
    println "**************************** during"
}

tasks.publish.dependsOn(aoeu)

tasks.publish.doFirst {
    println "************************* before"
}

tasks.publish.doLast {
    println "************************* after"
}

Its output is:

:aoeu
**************************** during
:publish
************************* before
************************* after

But what I really need is for the 'during' to happen between 'before' and 'after'. How can this be done? Should 'publish' simply depend on 'before'? If so, how can I guarantee that it happen before other dependencies?

like image 401
Noel Yap Avatar asked Sep 10 '13 23:09

Noel Yap


People also ask

Does Gradle run tasks in order?

In Gradle terms this means that you can define tasks and dependencies between tasks. Gradle guarantees that these tasks are executed in the order of their dependencies, and that each task is executed only once.

What are the 3 phases of the Gradle lifecycle?

The Gradle Lifecycle: A Review These phases are initialization, configuration, and execution. During the initialization phase, Gradle starts up and locates the build files it must process. Crucial during this phase is the determination of whether the build is single-project or multi-project.

Do you do first in Gradle?

initialize { doFirst { ... } } and initialize. doFirst { ... } are the exact same thing. Both statements are inserting an action at the front of the task's action list. Hence the action that gets inserted later (in this case further down in the script) will get executed first.

What does doLast do in Gradle?

Task doLast ( Closure action)Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed.


1 Answers

As far as I know your code should work propertly but it doesn't. doFirst should be executed in configuration phase which is run before doLast (execution phase). BTW this code works fine:

As Peter Niederwiser wrote here https://stackoverflow.com/a/9204159/2069368 doFirst run in execution phase as first statement (before doLast) so your code works fine. This example will show you execution order in gradle build:

task publish {
    println "(1) before (run in configuration phase)" // this will run before gradle dependencies
}

task aoeu << {
    println "(2) during  (run in execution phase as last statement)"
}

tasks.publish.dependsOn(aoeu)
tasks.publish.doLast {
    println "(4) after  (run in execution phase as last statement)"
}

tasks.publish.doFirst {
    println "(3) after  (run in execution phase as FORST statement - before doLast/<<)"
}

It will return

C:\>gradle publish
(1) before (run in configuration phase)
:aoeu
(2) during  (run in execution phase as last statement)
:publish
(3) after  (run in execution phase as FORST statement - before doLast/<<)
(4) after  (run in execution phase as last statement)

[UPDATE]

Here http://gradle.1045684.n5.nabble.com/task-run-order-lt-lt-syntax-doLast-doFirst-etc-td3337481.html is great question with great example which shows execution order.

Read this article about gradle lifecycle, it will help you understand it.

[UPDATE] If you want to run task aoeu in publish execution phase you can call aoeu.execute in publish.doFirst. But as far as I know it shouldn't be done in that way.

task publish {
}

task aoeu << {
    println "(2) during  (run in execution phase as last statement)"
}

tasks.publish.doLast {
    println "(3) after  (run in execution phase as last statement)"
}

tasks.publish.doFirst {
    println "(1) after  (run in execution phase as FORST statement - before doLast/<<)"
    aoeu.execute()
}

will return

C:\>gradle publish
:publish
(1) after  (run in execution phase as FORST statement - before doLast/<<)
(2) during  (run in execution phase as last statement)
(3) after  (run in execution phase as last statement)

As I see you want to run aoeu in the middle of publish task. I think that the best way to do it will be to divide publish task into two separate tasks and use depends, mustRunAfter to control execution order. Look at this example:

task publishInit << {
    println '(1)'
}

task aoeu << {
    println '(2)'
}

task publish << {
    println '(3)'
}

publish.dependsOn publishInit
publish.dependsOn aoeu
aoeu.mustRunAfter publishInit

It will return

C:\>gradle publish
:publishInit
(1)
:aoeu
(2)
:publish
(3)
like image 119
pepuch Avatar answered Sep 22 '22 14:09

pepuch