# Multiple excludes for multiple dependencies

**URL:** <https://discuss.gradle.org/t/multiple-excludes-for-multiple-dependencies/11573>\
**Category:** Help/Discuss\
**Created:** [September 10, 2015, 6:40pm UTC](https://discuss.gradle.org/t/multiple-excludes-for-multiple-dependencies/11573 "2015-09-10T18:40:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 10, 2015, 6:40pm UTC](https://discuss.gradle.org/t/multiple-excludes-for-multiple-dependencies/11573/1 "2015-09-10T18:40:51Z")

</div>

I need clarification on multiple excludes for multiple dependencies. For example, I want to exclude this from axis2:

```
compile ('org.apache.axis2:axis2-adb:1.6.3') {
        // axis2-kernel
        exclude module: 'axiom-compat'
        exclude group: 'javax.servlet'
        exclude group: 'commons-fileupload'
        exclude group: 'org.apache.woden'
        exclude group: 'javax.ws.rs'
        // axiom-api
        exclude module: 'jaxen'
        exclude group: 'org.apache.geronimo.specs'
        exclude module: 'apache-mime4j-core'
        // neethi
        exclude group: 'org.codehaus.woodstox'
    }

```

I want to apply all that excludes to this too:

```
compile 'org.apache.axis2:axis2-transport-local:1.6.3'
compile 'org.apache.axis2:axis2-transport-http:1.6.3'

```

From docs, I know I can use:

```
configurations {
    compile.exclude module: 'axiom-compat'
    compile.exclude group: 'javax.servlet'
    compile.exclude group: 'commons-fileupload'
    compile.exclude group: 'org.apache.woden'
    compile.exclude group: 'javax.ws.rs'
    // axiom-api
    compile.exclude module: 'jaxen'
    compile.exclude group: 'org.apache.geronimo.specs'
    compile.exclude module: 'apache-mime4j-core'
    // neethi
    compile.exclude group: 'org.codehaus.woodstox'
}

```

Two questions:

- I want to exclude all that dependencies **only for axis2 libraries**. What is the best way? I don’t want to duplicate the excludes. Also, by using `configurations` I might have problems if in the future some other library depends on one of those excluded libraries. I mean, an exclude in `configurations` is global and I only want those excludes for axis2 libraries.
- Is it possible to define multiple excludes in **one line** of code?

---

<div class="post-metadata">

**Author:** ![Shinya\_Mochida](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/shinya_mochida/32/2393_2.png) [@Shinya\_Mochida](https://discuss.gradle.org/u/Shinya_Mochida)\
**Post date:** [September 11, 2015, 4:33am UTC](https://discuss.gradle.org/t/multiple-excludes-for-multiple-dependencies/11573/2 "2015-09-11T04:33:56Z")

</div>

How about setting `compile` configuration’s `transitive` mode to `false`.

[https://docs.gradle.org/current/userguide/dependency\_management.html#sub:configurations](https://docs.gradle.org/current/userguide/dependency_management.html#sub:configurations)

---

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 11, 2015, 1:06pm UTC](https://discuss.gradle.org/t/multiple-excludes-for-multiple-dependencies/11573/3 "2015-09-11T13:06:40Z")

</div>

@Shinya_Mochida again, that’s not what I asked. Axis2 dependencies still have more than 10 transitive dependencies that I need.

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [September 11, 2015, 1:53pm UTC](https://discuss.gradle.org/t/multiple-excludes-for-multiple-dependencies/11573/4 "2015-09-11T13:53:49Z")

</div>

you can do something like the following, using Groovy beauty

```
dependencies {
  ['local','http'].each{
    compile ("org.apache.axis2:axis2-transport-$it:1.6.3"){
      ['axiom-compat','jaxen','apache-mime4j-core'].each {
        exclude module : "$it"
      }
      ['javax.servlet','commons-fileupload','org.apache.woden','javax.ws.rs','org.apache.geronimo.specs','org.codehaus.woodstox'].each {
        exclude group: "$it"
      }
    }
  }
}

```

but I’m not sure the end result is any better, in terms of comprehension

---

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 16, 2015, 10:02pm UTC](https://discuss.gradle.org/t/multiple-excludes-for-multiple-dependencies/11573/5 "2015-09-16T22:02:12Z")

</div>

Since it seems that there is no easy way to define multiple excludes for multiple dependencies, I followed @Francois_Guillot advise and I’m using this:

```
List axisExModules = ['axiom-compat', 'jaxen', 'apache-mime4j-core']
    List axisExGroups = [ 'javax.servlet', 'commons-fileupload', 'org.apache.woden',
                           'javax.ws.rs', 'org.apache.geronimo.specs', 'org.codehaus.woodstox' ]
    compile ('org.apache.axis2:axis2-adb:1.6.3') {
        axisExModules.each { exclude module : "$it" }
        axisExGroups.each { exclude group: "$it" }
    }
    compile ('org.apache.axis2:axis2-transport-local:1.6.3') {
        axisExModules.each { exclude module : "$it" }
        axisExGroups.each { exclude group: "$it" }
    }
    compile ('org.apache.axis2:axis2-transport-http:1.6.3') {
        axisExModules.each { exclude module : "$it" }
        axisExGroups.each { exclude group: "$it" }
    }
```

---

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 16, 2015, 11:25pm UTC](https://discuss.gradle.org/t/multiple-excludes-for-multiple-dependencies/11573/6 "2015-09-16T23:25:42Z")

</div>

Another option, using a method:

```
    ext.compileEx = { lib, exModules, exGroups ->
        compile (lib) {
            exModules.each { exclude module : "$it" }
            exGroups.each { exclude group: "$it" }
        }
    }
    
    List axisExModules = ['axiom-compat', 'jaxen', 'apache-mime4j-core']
    List axisExGroups = [ 'javax.servlet', 'commons-fileupload', 'org.apache.woden',
                           'javax.ws.rs', 'org.apache.geronimo.specs', 'org.codehaus.woodstox' ]
    compileEx ('org.apache.axis2:axis2-adb:1.6.3', axisExModules, axisExGroups)
    compileEx ('org.apache.axis2:axis2-transport-local:1.6.3', axisExModules, axisExGroups)
    compileEx ('org.apache.axis2:axis2-transport-http:1.6.3', axisExModules, axisExGroups)
```

---

<div class="post-metadata">

**Author:** ![radistao](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/radistao/32/6135_2.png) [@radistao](https://discuss.gradle.org/u/radistao)\
**Post date:** [August 13, 2018, 8:12am UTC](https://discuss.gradle.org/t/multiple-excludes-for-multiple-dependencies/11573/7 "2018-08-13T08:12:08Z")

</div>

Using _combinations_ might be implemented like this:

```gradle
/**
 * 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
    }
}

```
