如何使用Gradle在没有第一个目录的情况下提取

din*_*shr 8 groovy build unzip gradle

我正在尝试提取没有PARENT目录的从属zip文件,在使用Gradle提取时排除某些文件.

这是我得到的,这有效,但感觉不对,我希望有更好的方法来做到这一点

我提取的Zip文件

jar tf parent-folder-name.zip


parent-folder-name/bin/something.sh
parent-folder-name/bin/something.bat
parent-folder-name/lib/somelib.jar
Run Code Online (Sandbox Code Playgroud)

选项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()
    }
}
Run Code Online (Sandbox Code Playgroud)

选项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()
}
Run Code Online (Sandbox Code Playgroud)

对我来说选项2感觉更好,但我不确定在Gradle中是否有更好的方法可以做到这一点?

Gle*_*nnV 2

看来你的用例在 gradle 中还没有以非常用户友好的方式支持。这里列出了一个相关的功能请求。

还有一个类似的 stackoverflow 问题,其中有一些建议看起来比您已有的选项更容易。

因为 gradle 与 ant 集成得很好,而且 ant 解压缩任务确实支持扁平化,所以你也可以依赖它。

欲了解更多详情,请参阅: