Allprojects tasks and a task with that name already exists

I have projectA, projectB, projectC.

projectA and projectB both depend on projectC. projectA includes projectB as well.

I want to be able to run a task on allprojects when I build projectA or when I build projectB, as projectB needs to be able to be built independently of projectA, so I included “allprojects{ task myTask << {} }” in the build.gradle for projectA and projectB.

I get the following error in projectA:

Cannot add task ‘:projectB:myTask’ as a task with that name already exists.

If you want to define the task just for a single project, then there’s no need to define it within allprojects. What I’d rather do is to define the logic on in the root project of projectA and projectB e.g.

configure([project(':projectA'), project(':projectB']) {
    task myTask << {}
}

If that doesn’t help, please provide a sample project that reproduces the issue. I’d need to see the hierarchy and the build logic.