使用gradle Copy/Sync从zip提取时删除部分文件路径
给定一个声明为gradle依赖项的zip文件使用gradle Copy/Sync从zip提取时删除部分文件路径,gradle,Gradle,给定一个声明为gradle依赖项的zip文件 dependencies { orientdb(group: "com.orientechnologies", name: "orientdb-community", version: orientdbVersion, ext: "zip") } 其中包含以下结构中的文件 . └── orientdb-community-2.2.33 ├── benchmarks │ ├── bench_memory_get.bat
dependencies {
orientdb(group: "com.orientechnologies", name: "orientdb-community", version: orientdbVersion, ext: "zip")
}
其中包含以下结构中的文件
.
└── orientdb-community-2.2.33
├── benchmarks
│ ├── bench_memory_get.bat
│ └── post.txt
├── bin
│ ├── backup.sh
...
可以使用以下任务将zip内容同步到给定的目标目录中,以保留zip的完整结构:
task("deploy-db", type: Sync) {
from(configurations.orientdb.collect { zipTree(it) })
into(orientdbTgt)
}
如何将上述任务配置为从结果中删除“orientdb community-$orientdbVersion”
目录,以便输出为:
/${orientdbTgt}
├── benchmarks
│ ├── bench_memory_get.bat
│ └── post.txt
├── bin
│ ├── backup.sh
...
信息:
重命名((.*/)orientdb社区-$orientdbVersion/(.+),“$1$2”)
似乎不起作用,因为它只作用于文件名,这里的重命名涉及路径。使用Gradle 4.5.1,下面是一个合理的传真,可以工作
它在Sync
任务上使用eachFile
()功能。下面,我们更改FileCopyDetails
对象上通过eachFile
传递的路径
project.ext.orientdbTgt = 'staging'
project.ext.prefixDir = "orientdb-community-2.2.33${File.separator}"
task("deploy-db", type: Sync) {
from(configurations.orientdb.collect { zipTree(it) })
into(orientdbTgt)
eachFile { fileCopyDetails ->
def originalPath = fileCopyDetails.path
fileCopyDetails.path = originalPath.replace(prefixDir, "")
}
doLast {
ant.delete(dir: "${orientdbTgt}/${prefixDir}")
}
}
谢谢。有趣的是,除了
doLast
位之外,我同时也找到了相同的解决方案:你知道为什么原始树保持为空,那么为什么需要这个doLast
?不确定为什么,但在迭代文件之前设置文件夹似乎是合理的。Gradle不太可能确认所有文件都已转换为新的根目录。