Execute tasks sequentially in some task


(Alexey) #1

I have a library, which contains 3 lib modules and 1 example module. Before deploy task I want to execute some other tasks. In command line it looks like this: ./gradlew -x:example:clean -x:example:check -x:example:uploadArchives clean check :androidLib:assembleRelease uploadArchives.

I want to write gradle task to execute all tasks sequentially for all modules besides example module. That I can do: ./gradlew deployAll. How can i do it?

I try do this:

task deployAll {
doLast {
    subprojects {
         if(it.plugins.withType(com.android.build.gradle.AppPlugin)) return
         it.tasks.getByName('clean').execute()
         it.tasks.getByName('check').execute()
         ...
    }
}

}

But execute() is deprecated and it execute only first task and ignore any.


(Stefan Wolf) #2

Hi Alexey,

can you use dependsOn, mustRunAfter and shouldRunAfter to model your dependencies? IIUC, you want the deployAll task to depend on clean and check, right? Is that all you need to do?

Cheers,
Stefan


(Alexey) #3

Hi Stefan,

I want, that I can execute only my task deployAll and this executing all the tasks listed in it in order automatically for each subproject.


(Stefan Wolf) #4

What stops you from using dependsOn and shouldRunAfter? Does using those methods have some unwanted side effects for you?


(Alexey) #5

Can you help me, how correct to use these? Something like this?

task deployAll {
    doLast {
        subprojects {
             if(it.plugins.withType(com.android.build.gradle.AppPlugin)) return
             
             it.tasks.getByName('check') shouldRunAfter it.tasks.getByName('clean')
             // and then call it.tasks.getByName('clean').execute() ? or how?
             ...
        }
    }
}

(Stefan Wolf) #6

I was more thinking in terms of:

task deployAll {
    dependsOn(subprojects*.tasks*.getByName('clean'))
    dependsOn(subprojects*.tasks*.getByName('check'))
}

When you now run deployAll, the clean and check tasks will be executed, too.

The if statement in you subproject block is there since you don’t want to depend on projects which have the AppPlugin applied?


(Alexey) #7

Thanks! I will try this solution.

Yes, I don’t want to execute these tasks in project, which have the AppPlugin.