How to replace group when the group name is not known in advance

I have this particular situation where the dependency tree could contain two dependencies with the same artifact name but different groups. For example, in different places in the dependency tree I could have:

com.domain.groupA:artifactName1:1.2.3   # Older version of desired group
....
com.domain.groupA:artifactName1:1.2.4   # Latest version of desired group (desired version)
...
com.domain.groupB:artifactName1:2.3.4   # Undesired group, should be replaced by "Latest version"

The problem is I want to replace all non “groupA” groups with the resolved dependency for com.domain.groupA:artifactName1 (which would be version 1.2.4), but I actually do not know ahead of time the other group names that I will be facing and there could be many of them, so they cannot be excluded by name.

I guess you need something like this, just not checking for group and version, but for artifact name, or artifact name while group is not the desired one: Using Resolution Rules

I see that the dependencySubstitution API can substitute a dependency for another. However, I am looking to completely exclude a resolved dependency if it does not match a group name. I cannot use the exclude API in configurations API because I dont know the group name before I see the dependency graph. Is that possible?

I don’t know, never tried something like that. I guess yes, but can’t tell you how from the top of my head.