Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Flatten The Directory Structure When UnZipping With Gradle?

Tags:

gradle

I want to extract some jars from a 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 dir in the outputDir. I just want the jars flat.

To do it in Ant I would do this:

  <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 do I do it in gradle?

like image 404
opticyclic Avatar asked Aug 02 '14 05:08

opticyclic


People also ask

How do I UnZip a file in Gradle?

But there is no UnZip or UnTar to unpack an archive in Gradle. To unpack an archive we must use the Copy task and copy the contents of the archive to a specified destination directory. In Gradle we can use the zipTree() method to access the contents of an archive.

How do I create a tar file in Gradle?

You can run gradle distZip to package the main distribution as a ZIP, or gradle distTar to create a TAR file. To build both types of archives just run gradle assembleDist . The files will be created at $buildDir/distributions/${project.name}-${project. version}.

How do I create a directory in Gradle?

Creating directories All core Gradle tasks ensure that any output directories they need are created if necessary using this mechanism. As described in the Apache Ant manual, the mkdir task will automatically create all necessary directories in the given path and will do nothing if the directory already exists.

How do I open a Gradle file?

If you are already using Gradle with your IntelliJ project, you can open it in Android Studio using the following steps: Click File > New > Import Project. Select your IntelliJ project directory, and click OK. Your project will open in Android Studio.


4 Answers

I tried the answer by Al J, but it didn't work with the 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
}
like image 53
AngelWarrior Avatar answered Sep 29 '22 18:09

AngelWarrior


I believe you can simply append .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'
}
like image 37
al. Avatar answered Sep 29 '22 17:09

al.


It seems like there is a way with Gradle. It is more complicated though and not necessarily that 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 makes sure that extra dirs aren't copied.

The eachFile block flattens the structure (you have to put the name into an array to make it work).

like image 30
2 revs Avatar answered Sep 29 '22 16:09

2 revs


It seems like this is a bug/missing feature in Gradle http://issues.gradle.org/browse/GRADLE-3025

For now I am having to delegate to good old reliable Ant to do the work!

task unzip(){
  ant.unzip(src: 'D:/external/dependent.war', dest:'lib', overwrite:"true") {
    patternset( ) {
      include( name: 'WEB-INF/lib/*.jar' )
    }
    mapper(type:"flatten")
  }
}
like image 32
opticyclic Avatar answered Sep 29 '22 17:09

opticyclic