How to smooth directory structure when unpacking using Gradle?

I want to extract some cans from the war as part of my gradle (2.0) build. So far I have this:

task unzip(type: Copy) { def zipFile = file('D:/external/dependent.war') def outputDir = file('lib') from zipTree(zipFile) into outputDir include 'WEB-INF/lib/*.jar' } 

This puts the WEB-INF / lib directory in outputDir. I just want the cans to be flat.

To do this in Ant, I would do the following:

  <target name="unzip"> <unzip src="D:/external/dependent.war" dest="lib"> <patternset> <include name="WEB-INF/lib/*.jar"/> </patternset> <mapper type="flatten"/> </unzip> </target> 

How to do it in gradle?

+7
gradle
source share
4 answers

I believe you can just add .files to your zipTree .

 task unzip(type: Copy) { def zipFile = file('D:/external/dependent.war') def outputDir = file('lib') from zipTree(zipFile).files into outputDir include 'WEB-INF/lib/*.jar' } 
+4
source share

This seems to be a bug / missing feature in Gradle http://issues.gradle.org/browse/GRADLE-3025

At the moment, I have to delegate the good old reliable Ant to get the job done!

 task unzip(){ ant.unzip(src: 'D:/external/dependent.war', dest:'lib', overwrite:"true") { patternset( ) { include( name: 'WEB-INF/lib/*.jar' ) } mapper(type:"flatten") } } 
+3
source share

There seems to be a way with Gradle. This is more difficult, although not necessary, which is obvious.

 task unzip(type: Copy) { def zipFile = file('D:/external/dependent.war') def outputDir = file('lib') from zipTree(zipFile) into outputDir include '**/*.jar' includeEmptyDirs false eachFile { FileCopyDetails fcp -> // remap the file to the root String[] segments = [fcp.getName()] fcp.relativePath = new RelativePath(true, segments) } } 

includeEmptyDirs false ensures that additional drives are not copied.

The eachFile block blocks the structure (you must put the name in an array to make it work).

+2
source share

I tried to answer Al J, but it did not work with include.

This worked for me:

 task unzip(type: Copy) { def zipFile = file('D:/external/dependent.war') def outputDir = file('lib') from zipTree(zipFile).matching{include 'WEB-INF/lib/*.jar'}.files into outputDir } 
+2
source share

All Articles