How do I reuse/copy a CopySpec without modifying the original?

I want to explode a JAR file immediately after creating it, but I only want certain files in the exploded copy. I thought this would do the trick:

apply plugin: 'java'
  task explode(type: Copy) {
    into "unzipped"
    with jar {
  exclude "*.html"
    }
}
  jar {
 from("resourceDirectory") {
  include "*.html"
  include "*.js"
 }
}

However, without even calling the “explode” task, the JAR file doesn’t include the HTML files. Does exclude in that task override the include in the “jar” task during the configuration phase? Is there an easy way to modify the “jar” CopySpec without modifying the copy of it used in the task?

Or is my only option to provide a path to the actual exploded JAR?

Your code is modifying the ‘jar’ task at configuration time, and ‘exclude’ wins over ‘include’. Try:

task explode(type: Copy) {
  into "unzipped"
  exclude "*.html"
  with jar
}

Peter,

That does fix the problem of the HTML files being (incorrectly) excluded from the JAR. But the HTML files still end up in the exploded copy. I.e., the Copy task is not honoring the exclude parameter.

Ryan

I uploaded a short code sample here to illustrate. This isn’t mission-critical (I can unzip the entire archive if necessary), but it would be nice to know if it’s possible.

http://www.mediafire.com/?dyzjhb8cbetqy7d

I’m resurrecting this thread since I’m having the same problem. In my case, i basically need to create a jar that has the same contents of a war, except for the WEB-INF/lib folder. It seems reasonable to make use of the war task’s copyspec, so i tried this:

task warFileset(type: Jar, dependsOn: war) {
            with war
            exclude 'WEB-INF/lib/'
        }

But the exclude has no effect. I’m able to get around it by pulling content from the war archive directly like this:

task warFileset(type: Jar, dependsOn: war) {
            from zipTree(war.archivePath)
            exclude 'WEB-INF/lib/'
        }

But I’m hoping there’s a better way. Is there?