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

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)

Option 2

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

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.