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

2023-12-22

我正在尝试提取一个不带 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

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感觉好多了,但我不确定在 Gradle 中是否有更好的方法来做到这一点?


看来你的用例在 gradle 中还没有以非常用户友好的方式支持。列出了相关的功能请求here https://github.com/gradle/gradle/issues/1108.

还有这个类似的计算器问题 https://stackoverflow.com/questions/25091843/how-do-i-flatten-the-directory-structure-when-unzipping-with-gradle其中有一些建议看起来比您已有的选项更容易。

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

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

  • 使用 gradle 中的 ant https://docs.gradle.org/current/userguide/ant.html
  • Ant解压任务 https://ant.apache.org/manual/Tasks/unzip.html
  • 展平映射器 https://ant.apache.org/manual/Types/mapper.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Gradle 在没有第一个目录的情况下提取? 的相关文章

随机推荐