Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract without first directory using Gradle?

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?

like image 764
dineshr Avatar asked Apr 03 '14 17:04

dineshr


1 Answers

Seems your use case is not supported in a very userfriendly way in gradle yet. There is a related feature request listed here.

There also this similar stackoverflow question which has a few suggestions that look easier than the options you already have.

Because gradle integrates well with ant, and the ant unzip task does support flattening you can also rely on that.

For more details see:

  • Using ant from gradle
  • Ant unzip task
  • flatten mapper
like image 112
GlennV Avatar answered Oct 20 '22 01:10

GlennV