Same class paths from two different jars

I have a java project where I pull in two jars A and B, using implementation under dependencies in my build.gradle file. Both A and B have unique dependencies that they pull in as jars, X and Y respectively. Unfortunately, both X and Y have the same class paths. The class paths don’t have any versioning tied in, so how can I tell gradle to build A with X and B with Y?

I’ve been reading up on documentation, specifically Working with Dependencies, and some options come close but none of them get me to a solution. I’m using the latest gradle 7.2

AFAIK; The java compiler does not support such a thing. All of the dependencies end up on the compiler classpath. If multiple jars contain the same classes, then whichever jar is on the classpath first will be checked first when loading.

Reading through Handling mutually exclusive dependencies specifically the section Selecting between candidates (gradle.org) makes it seem like it should be possible. The example of the logger is similar to my problem, it seems like I just need to change some of the syntax to work for specifying a jar for and it would work.