如何使用Gradle首先提取目录? [英] How to extract without first directory using Gradle?
问题描述
我试图提取一个不带PARENT目录的依赖zip文件,在使用Gradle提取时排除某个文件。
这是我我已经得到了,这工作,但感觉不对,我希望有一个更好的方式来做到这一点
我解压缩的Zip文件
jar tf parent-folder-name.zip
<
父文件夹-name / bin / something.sh
父文件夹名/ bin / something.bat
父文件夹名/ lib / somelib.jar
任务explodeToDist1(类型:Copy){从zipTree(configurations.extractDist.singleFile)
排除** / lib / **
eachFile {
def newPath = it。 ('/)
it.relativePath = RelativePath.parse(true,newPath)
}
到'build / dist'
doLast {
def path = buildDi r.getPath()+dist / parent-folder-name
def dirToDelete = new File(path)
dirToDelete.deleteOnExit()
}
}
选项2
任务explodeToDist2<< {
def unzipDir = new File('build / unzipTmp')
copy {
from zipTree(configurations.extractDist.singleFile)
into unzipDir
}
def rootZipDir = unzipDir.listFiles()[0]
fileTree(rootZipDir){
排除** / lib / **
} .copy {
到'src / dist'
}
unzipDir.deleteDir()
}
对我来说选项2 感觉更好,但我不确定在Gradle中是否有更好的方法来完成这项工作?
似乎您的用例在gradle中并不支持用户友好的方式。在此处列出了相关的功能请求。
还有这个类似stackoverflow问题,其中有一些建议比现有选项更容易。
由于gradle与ant很好地集成,并且ant unzip任务支持
欲了解更多详情,请参阅:
- 从gradle使用ant
- < a href =https://ant.apache.org/manual/Tasks/unzip.html =nofollow noreferrer>解压缩任务
- 拼合贴图
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?
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:
这篇关于如何使用Gradle首先提取目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!