How can I exclude a dependency from a top-level project in a leaf project while building a zip?

I have a base project that builds a jar and then leaf projects that build from that and package up a zip for distribution to a server. Some of the base project’s dependencies should be packaged into the leaf project’s zip, but others are provided on the server and should not be. I’m not sure how to exclude those dependencies and their transitive dependencies?

My setup looks something like: Root build.gradle:

List A ["org.acme.blah", "org.acme.blahblah"]
dependencies {
  compile A
  compile('B') {
    exclude group 'these.guys.suck'
  }
  compile C,D,E
}

Leaf project build.gradle:

dependencies {
  compile project(':')
}
  task distForServer(type: Zip) {
  from configurations.runtime,
       configurations.default.allArtifacts.files,
       sourceSets.main.resources
  exclude A,B <-- (and their dependencies) How to do this?
}

I need the distForServer artifact to include :'s artifacts, the leaf project’s artifacts, C,D,E and C,D,E’s transitive dependencies. I know it’s not preferred to have code in ‘:’, and if necessary I can re-factor, but it’d be better to leave as is.

Am I missing something simple here? Thanks.

Not sure I understand your scenario entirely, but let’s see how far this gets you. The configuration interface has an exclude() method.

Given the following standalone build.gradle:

apply plugin: "groovy"
  repositories {
  mavenCentral()
}
  dependencies {
  groovy "org.codehaus.groovy:groovy:1.8.6"
}
  task distWithExclude(type: Zip) {
  baseName "exclude"
  from configurations.runtime.copyRecursive().exclude(group: 'asm'),
       configurations.default.allArtifacts.files,
       sourceSets.main.resources
}
  task distNormal(type: Zip) {
  baseName "normal"
  from configurations.runtime,
       configurations.default.allArtifacts.files,
       sourceSets.main.resources
}

and the following shell session:

…first to get a feel for the dependencies before we exclude anything, print them:

$ gradle dependencies
:dependencies
  ------------------------------------------------------------
Root project
------------------------------------------------------------
...
runtime - Classpath for running the compiled main classes.
\--- org.codehaus.groovy:groovy:1.8.6
     +--- antlr:antlr:2.7.7
     +--- asm:asm:3.2
     +--- asm:asm-commons:3.2
     |
  \--- asm:asm-tree:3.2
     |
       \--- asm:asm:3.2 (*)
     +--- asm:asm-util:3.2
     |
  \--- asm:asm-tree:3.2 (*)
     +--- asm:asm-analysis:3.2
     |
  \--- asm:asm-tree:3.2 (*)
     \--- asm:asm-tree:3.2 (*)
...

…run the two zip tasks:

$ gradle dWE dN
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:distWithExclude
:distNormal
  BUILD SUCCESSFUL
  Total time: 3.0 secs

…and finally take a look at the zip content:

$ unzip -l build/distributions/normal.zip
 Archive:
build/distributions/normal.zip
  Length
    Date
  Time
  Name
---------
---------- -----
 ----
  5546084
2012-09-04 19:03
 groovy-1.8.6.jar
   445288
2012-09-04 19:03
 antlr-2.7.7.jar
    43398
2012-09-04 19:03
 asm-3.2.jar
    21878
2012-09-04 19:03
 asm-tree-3.2.jar
    33094
2012-09-04 19:03
 asm-commons-3.2.jar
    36551
2012-09-04 19:03
 asm-util-3.2.jar
    17985
2012-09-04 19:03
 asm-analysis-3.2.jar
      261
2012-10-17 22:50
 gradle-transitive.jar
---------
                   -------
  6144539
                   8 files
  $ unzip -l build/distributions/exclude.zip
 Archive:
build/distributions/exclude.zip
  Length
    Date
  Time
  Name
---------
---------- -----
 ----
  5546084
2012-09-04 19:03
 groovy-1.8.6.jar
   445288
2012-09-04 19:03
 antlr-2.7.7.jar
      261
2012-10-17 22:50
 gradle-transitive.jar
---------
                   -------
  5991633
                   3 files

Note the copyRecursive call before the exclude call. Without this we actually make the exclude global, i.e. if you run the two tasks after each other (and the distWithExclude task executes first) then both zip files will have the smaller foot print…since we told gradle to modify the ‘runtime’ configuration and exclude everything with the group ‘asm’.

Gradle gurus, feel free to chime in if there is a cleaner way…and feel free to let me know if this solves your problem or if I misunderstood your issue.

This works great. Thanks.