How to execute a task before executing or building the subprojects in gradle?

How to execute a task before executing or building the subprojects in gradle?

I have a multi-project build with the following build.gradle in the root project

apply plugin: 'com.google.protobuf'


allprojects {

}

subprojects {

}

protobuf {

   protoc {
    // Download from repositories
    artifact = 'com.google.protobuf:protoc:3.10.0'
   }

   plugins {
      grpc {
        artifact = 'io.grpc:protoc-gen-grpc-java:1.23.0'
      }
   }

   generateProtoTasks {
      ofSourceSet('main')*.plugins {
        grpc {}
      }
   }
}

I want the protobuf tasks to be executed first before compiling any of the subprojects because my subprojects depends on the generated java files from protobuf task. so how can I achieve this? I don’t want to have the protobuf task in each subproject instead I just want to do it at once at the root project but before compiling the subprojects.