Gradle dependencyResolutionManagement versionCatalogs exclude transitive

I have a pattern in my codebase where I have libraries created in a map, and it appears to operate very similar to how the versionCatalogs functionality works. I’m trying to migrate to the gradle way of doing things rather than having my one off way of doing things. I’m not quite sure how to exclude transitive dependencies.

In my home rolled way of doing it, I have something like this:

def libraries = [
   groovy_all: dependencies.create("org.codehaus.groovy:groovy-all:3+") {
          exclude group: "org.codehaus.groovy", module: "groovy-swing"
    } 
]

I’m trying something like this in the new way of doing things, but it isn’t working as I expect:

dependencyResolutionManagement {
    versionCatalogs {
        create("libs") {
            library("groovy-all", "org.godehaus.groovy:groovy-all:3.0.9") {
                     exclude(group = "org.codehaus.groovy", module = "groovy-swing")
             }
        }
    }
}

You cannot exclude transitive dependencies in version catalogs. Version catalogs are like their names suggest just a catalog of libraries to pick from. You could also not declare attributes or configurations or artifacts and so on. These things are fine where you use the library from the catalog. Or you can use a platform to control dependencies, including transitive ones. There you can also use excludes. Both features also work hand-in-hand. You can define a platform where you use the versions from the catalog.