How to extract without first directory using Gradle?

做~自己de王妃 提交于 2019-12-10 03:04:18

问题


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:


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


来源:https://stackoverflow.com/questions/22844296/how-to-extract-without-first-directory-using-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!