Exclude transitive dependency

I have a direct dependency on selenium-java:4.11.0. Another package MyPackage also has a dependency on selenium-java:3.141.0. It is always resolving selenium-java:3.141.0. I tried with transitive: false and excluding selenium-java for MyPackage but still is getting resolved.

    compileOnly('MyPackage:latest.release') {
        exclude group: 'org.seleniumhq.selenium'

If my files are in the test folder, it is resolving 4.11.0 but inside the main folder, it resolves to 3.141.0.

Can anyone please guide what is wrong here?

Can you provide a build --scan?
You can also try to use the dependencies and dependencyInsight tasks to gather information on where the version is coming from.

One possibility for example is, that you use the legacy Spring dependency management plugin together with a BOM that defines that 3.141.0 version. Even the maintainer of that plugin recommends not to use it anymore but instead use the Gradle built-in BOM support.