How to run a task in the root project before compiling or running any of the subprojects?

How to run a task in the root project before compiling or running any of the subprojects?

More specifically, I want to execute a proto(generate code from protocol buffers spec) task at the root project before even compiling any of the subprojects. How do I do that?

Any equivalent example would suffice as well.