How to extract without first directory using Gradle?


(Dinesh Ranganathan) #1

I am trying extract a dependant zip file without the PARENT directory, exclude some file while extracting using Gradle.

Here is what I’ve got and this works but doesn’t feel right and I am hoping there is a better way to do this

Zip file that i am extracting

jar tf parent-folder-name.zip
                parent-folder-name/bin/something.sh
    parent-folder-name/bin/something.bat
    parent-folder-name/lib/somelib.jar

Option 1

task explodeToDist1(type: Copy) {
        from zipTree(configurations.extractDist.singleFile)
        exclude "**/lib/**"
            eachFile {
                def newPath = it.relativePath.segments[1..-1].join("/")
                it.relativePath = RelativePath.parse(true, newPath)
            }
        into 'build/dist'
        doLast {
            def path = buildDir.getPath() + "/dist/parent-folder-name"
            def dirToDelete = new File(path)
            dirToDelete.deleteOnExit()
        }
    }

Option 2

task explodeToDist2 << {
            def unzipDir = new File('build/unzipTmp')
            copy {
                from zipTree(configurations.extractDist.singleFile)
                into unzipDir
            }
            def rootZipDir = unzipDir.listFiles()[0]
            fileTree(rootZipDir){
                    exclude "**/lib/**"
            }.copy {
                into 'src/dist'
            }
            unzipDir.deleteDir()
    }

To me Option 2 feels better but I am not sure if there is a better way to do this in Gradle?


(Peter Niederwieser) #2

Unless it’s OK to hardcode the names of the subdirectories to extract, I don’t think there is a fundamentally better way.