Multiple excludes for multiple dependencies


(Ivan RF) #1

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?

(Shinya Mochida) #2

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

https://docs.gradle.org/current/userguide/dependency_management.html#sub:configurations


(Ivan RF) #3

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


(François Guillot) #4

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


(Ivan RF) #5

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" }
    }

(Ivan RF) #6

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)

(radistao) #7

Using combinations might be implemented like this:

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