Removing dependencies from a jar file during jar task


(Eugene Dvorkin) #1

I am trying to build a jar file with all runtime dependencies but exclude certain group. How to accomplish in in Gradle? Something like in Maven

<exclude>commons-lang:commons-lang</exclude>

My code is:

task stormJar(type: Jar) {
      archiveName "CPStorm.jar-${version}.jar"
    dependsOn configurations.runtime
    from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
    exclude('**/*storm*.jar')
    exclude('**/defaults.yaml')
    /*configurations.runtime {
        all.collect { configuration ->
            configuration.exclude group: 'storm', module: 'storm'
        }
    }*/
}

But instead storm.jar I would like to exclude whole dependency because it is already exists on server:

compile 'storm:storm:0.8.2'

basically I am trying to accomplish this task but with Gradle: https://github.com/nathanmarz/storm/wiki/Running-topologies-on-a-production-cluster


#2

Are you trying to create a ‘fat-jar’ with all of the dependency classes included directly? It looks like the ‘storm’ dependency is what’s referred to in Maven as ‘provided’ scope. In Gradle you can accomplish something similar by using a separate configuration.

Depending on what you’re trying to do, you could try manipulating configurations as described in GRADLE-784, or simply filter ‘storm*.jar’ out of the fat-jar inputs. (You’re excludes above will be attempting to match on the class files that will end up in the archive, not the input files that provide them).

task stormJar(type: Jar) {
    archiveName "CPStorm.jar-${version}.jar"
    from {
          configurations.runtime.filter( {! (it.name =~ /storm.*\.jar/ )}).collect {
             it.isDirectory() ? it : zipTree(it)
         }
     }

Or if you add the storm dependencies to an extra configuration, and subtract these from the inputs:

configurations {
    storm
    compile.extendsFrom storm
}
dependencies {
    storm 'storm:storm:0.8.2'
}
task stormJar(type: Jar) {
    archiveName "CPStorm.jar-${version}.jar"
    from {
         (configurations.runtime - configurations.storm).collect {
             it.isDirectory() ? it : zipTree(it)
         }
     }

I haven’t tested either of these :). But something like this should work.


(Eugene Dvorkin) #3

Thank you. You second suggestion is working. I was able to create fat jar but without storm dependencies.

jar {
    archiveName "CPStorm-${version}.jar"
    dependsOn configurations.runtime
    from {
        (configurations.runtime - configurations.storm).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

(James Sumners) #4

I have to say, this is really stupid:

configurations {
  provided
  compile.extendsFrom provided
}
  dependencies {
  compile 'org.slf4j:slf4j-api:1.7.5'
      provided 'org.codehaus.groovy:groovy-all:2.1.8'
    runtime 'com.oracle:ojdbc16:11.2.0.4'
  runtime 'ch.qos.logback:logback-classic:1.0.13'
}
  jar {
  dependsOn configurations.runtime
  from {
    (configurations.runtime - configurations.provided).collect {
      it.isDirectory() ? it : zipTree(it)
    }
  }
}

Why not just add a “provided” to the default configurations?