2012-07-25 36 views
0

我有這樣的結構在一個zip文件提取的文件夾,並創建一個jar

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

我希望能夠與.class文件只提取文件夾:COM,組織,機器人。並把它們放在一個罐子裏。 到目前爲止,我已經做到了這一點:

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')  
    } 
} 

但我得到它的類文件夾中的罐子,如:classes/org/apache/http/entity/mime/content/StringBody.class ,我希望它這樣的:org/apache/http/entity/mime/content/StringBody.class

任何想法如何? 謝謝!

回答

2

因此,我認爲是可以讓你更接近你想要的兩兩件事:

  1. 使用zipTree直接在jar任務,而不是創建一箇中間拉鍊。
  2. 使用eachFile更改每個文件的相對路徑以剝離第一個文件夾。
jar { 
    from zipTree('runtime.jar') { 
    include 'classes/**/*.class' 
    eachFile { it.relativePath = it.relativePath.segments[1..-1] } 
    } 
} 

罐的任務都的CopySpec方法,其提供eachFile能力。

+1

使用Gradle(1.11)的當前版本,此解決方案無法運行,您將得到一個ClassCastException,因爲這些段不能被轉換爲RelativePath。我有一個類似的問題,有一些替代解決方案。請查看這裏[如何提取沒有第一個目錄使用Gradle?](http://stackoverflow.com/questions/22844296/how-to-extract-without-first-directory-using-gradle?noredirect=1#comment34866075_22844296) – dineshr 2014-04-04 06:41:56

相關問題