Expressing dependency exclusions in a concise way

We have the following dependencies in our project:

compile(group: "org.springframework.security", name: "spring-security-config", version: springSecurityVersion) {
        exclude(module: "spring-expression")
        exclude(module: "spring-aop")
        exclude(module: "spring-jdbc")
        exclude(module: "spring-core")
        exclude(module: "spring-beans")
        exclude(module: "spring-tx")
    }
    compile(group: "org.springframework.security", name: "spring-security-taglibs", version: springSecurityVersion) {
        exclude(module: "spring-expression")
        exclude(module: "spring-aop")
        exclude(module: "spring-jdbc")
        exclude(module: "spring-core")
        exclude(module: "spring-beans")
        exclude(module: "spring-tx")
    }

Is there a way to express the rather long list of exclusions in one place and then reference it somewhere else? It’s quite a few lines that are duplicated.

There are two ways you can do it. Firstly, you are reusing the same closure that does the configuration so why not assign it to a variable?

def springSecurityDependencyConfig = {
   exclude(module: "spring-expression")
   exclude(module: "spring-aop")
   exclude(module: "spring-jdbc")
   exclude(module: "spring-core")
   exclude(module: "spring-beans")
   exclude(module: "spring-tx")
}
compile(group: "org.springframework.security", name: "spring-security-config", version: springSecurityVersion,
springSecurityDependencyConfig)
compile(group: "org.springframework.security", name: "spring-security-taglibs", version: springSecurityVersion, springSecurityDependencyConfig)

Another option is to extract the list of dependencies to a list:

def excludedSpringSecurityDependencies = ["spring-expression", "spring-aop", ...]
compile(group: "org.springframework.security", name: "spring-security-config", version: springSecurityVersion) {
    excludedSpringSecurityDependencies.each { exclude(module: it) }
}
compile(group: "org.springframework.security", name: "spring-security-taglibs", version: springSecurityVersion) {
    excludedSpringSecurityDependencies.each { exclude(module: it) }
}

You can also combine the two together.

1 Like

Thank you!

Another option is to exclude the dependencies globally (‘configurations.compile { exclude … }’ or ‘configurations.all { exclude … }’, not just for that particular dependency(s).