Executing a script only once at the beginning of a multi-project build


I have a multi-project build.

P1 (root)
-> C1 (child1)
-> C2 (child2)

P1 has a task buildAll, which includes C1:build & C2:build.

Before starting any of the tasks (e.g., buildAll, C1:build or C2:build), I want to execute a script only once.

I inserted the following in the settings.gradle, located at the root.

gradle.projectsLoaded {
println ‘All projects loaded’

However, when I execute ‘gw buildAll’, I see ‘All projects loaded’, printed twice.

Why and how can I invoke the script only once?