Adding a task with buildSrc for rootProject only possible?

Hi,
I would like to add a task only for the rootProject in a multiModule project using buildSrc. That works in theory well with the following:

if (!rootProject.tasks.getNames().contains("theTask")) {
  rootProject.tasks.register("theTask", Task::class) {
    
  }
}

But there is currently a java.util.ConcurrentModificationException because of course “all” modules want to create the task at the same time. How can I do this better? Can I find out if I am currently in the rootProject? Unfortunately I get

Function invocation 'project(...)' expected
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public fun DependencyHandler.project(path: String, configuration: String? = ...): ProjectDependency defined in org.gradle.kotlin.dsl

with the following call:

if (this.project.parent == null) {}

Thank you very much!

What are you creating in buildSrc? A plugin or precompiled script plugin? If so, only apply it to the root project.

Thank you, I’m creating a precompiles script. So you mean I should split the “convention” in two "conventions and apply the one in the root and the other in the submodules?

This sounds logical, but it would be better to have the related code in one convention. Maybe someone has another idea?
But yes, that would solve the problem.

Checking for a null parent should work, although I don’t know what this is in your example.
Could also do:

if( project == project.rootProject ) {
   // apply root-only conventions
}