How to exclude multiple groups when using dependencies with gradle

Example is here:

compile group: 'org.jitsi', name: 'libjitsi', version: '1.0-9-g4b85531', {
    [new Tuple('org.opentelecoms.sdp', 'sdp-api'),
     new Tuple('junit', 'junit')].each {
        exclude group: "${it.get(0)}", module: "${it.get(1)}"
    }
}

Using this solution with Groovy combinations (Cartesian product) to exclude some list from all configurations:

/**
 * These artifacts must be excluded from all configurations and dependencies.
 */
final excludedModules = [
    'module-to-exclude-1', 'module-to-exclude-2'
]


/**
 * Exclude dependencies from all configurations.
 * This configuration solves an issue 
 * when the same transitive dependency is included by different libraries.
 */
configurations {
    [all, excludedModules].combinations { config, moduleToExclude ->
        config.exclude module: moduleToExclude
    }
}

Well basically 'exclude' is just a method belongs to 'ModuleDependency' class which accepts a 'Map' of 'group' and 'module' and there's no way to pass more.

However you can use 'Groovy' power in this case and for each 'group' from list to call method 'exclude' on 'ModuleDependency' and to pass current 'group'. Take a look at approximate code below.

compile() { dep ->
    [group1, group2].each{ group -> dep.exclude group: group }
}

Tags:

Groovy

Gradle