How to extract without first directory using Gradl

2019-06-18 03:24发布

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?

1条回答
Summer. ? 凉城
2楼-- · 2019-06-18 03:49

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:

查看更多
登录 后发表回答