This can be only done in the buildscript itself, like
implementation(libs.foo.bar) { exclude(group = "baz", module = "bag") }