Dayerman
Dayerman

Reputation: 4003

Extract folders in gradle and create a jar

I have this structure in a zip file

classes
 |--com: A.class, B.class      
 |--org: C.class, D.class
 |--android: X.class
 |--stuff: Stuff.inf, info.txt

I want be able to extract only the folders with .class files: com, org, android. And put them on a jar. So far I have done this:

task createJar {
    //unzip the file
    FileTree zip = zipTree('runtime.jar') 
    FileTree zip2 = zip.matching {
        include 'classes/**/*.class'        
    }
    zip2.each { file -> println "doing something with $file" }      

    //create the jar
    jar{        
        from zip2
        destinationDir  file('GradleTests/testResult')      
    }
}

But I get the jar with the classes folder on it, like: classes/org/apache/http/entity/mime/content/StringBody.class And i want it like: org/apache/http/entity/mime/content/StringBody.class

Any idea how? Thanks!

Upvotes: 0

Views: 3082

Answers (1)

ajoberstar
ajoberstar

Reputation: 2585

So I think there are two main things that can get you closer to what you want:

  1. Use the zipTree directly in the jar task rather than creating an intermediate zip.
  2. Use eachFile to change the relative path of each file to strip off the first folder.
jar {
  from zipTree('runtime.jar') {
    include 'classes/**/*.class'
    eachFile { it.relativePath = it.relativePath.segments[1..-1] }
  }
}

Jar tasks have all of the CopySpec methods, which provide the eachFile abilities.

Upvotes: 2

Related Questions