Multiple invocations of "allprojects {}"

This depends on how the plugin is written, it’s certainly possible to write plugins where order is important. Usually, Gradle provides an API such that you can code your plugin to work the same regardless of execution order.

Let’s consider project.tasks which is an instance of TaskCollection, as with most of the collections in the Gradle model this extends DomainObjectCollection which has “live” methods including matching(Closure) and all(Closure). These will “Executes the given closure against all objects in this collection, and any objects subsequently added to this collection”

But, TaskCollection also extends Collection which has non-live methods such as findAll(Closure) and each(Closure). These Closures will only execute against the current objects in the collection and won’t apply to objects subsequently added to the collection.

The first code block below is written in a style where order is not important, it will print

“hello task1”
“hello task2”

task task1 {...}

tasks.matching { 
   it.name.startsWith('task') 
}.all { 
   println "hello $it.name" 
}

task task2 {...}

The second code block below is written in a style where order is important, it will print

“hello task1”

task task1 {...}

tasks.findAll { 
   it.name.startsWith('task') 
}.each { 
   println "hello $it.name" 
}

task task2 {...}

Plugin authors should always aim to use the “live” methods as shown in the first code block