英文:
Gradle Zip: retain the directory structures
问题
我创建了以下方式的 Java 文件的 Zip 压缩包,但它不会保留 `.zip` 中的目录结构。
```groovy
task ('apiJavaDocSources', type: Zip) {
from fileTree('core-module/src/mypackage/').matching {
'subpackage/*Exception.java'
'subpackage/package-info.java'
'anotherpackage/*.java'
}
from fileTree('lib-module/src/mypackage/').matching {
'bundleinfo/*Dummy.java'
'bundleinfo/Random.java'
}
archiveName 'api-javadoc-sources.zip'
destinationDir(file('image'))
}
我想要保留 zip 文件中的目录结构,如下所示:
api-javadoc-sources.zip
-- core-module
-- /src/mypackage/
-- subpackage/*.java
-- anotherpackage/*.java
-- lib-module
-- /src/mypackage/
-- bundleinfo/*.java
但是,我的实现只是将所有的 *.java
文件放在了 .zip
根目录下,如何在 zip 文件中保留目录结构呢?
<details>
<summary>英文:</summary>
I'm creating the Zip of my java files as below but it doesn't retain the directory structure in the `.zip`
task ('apiJavaDocSources', type: Zip) {
from fileTree('core-module/src/mypackage/').matching {
'subpackage/*Exception.java'
'subpackage/package-info.java'
'anotherpackage/*.java'
}
from fileTree('lib-module/src/mypackage/').matching {
'bundleinfo/*Dummy.java'
'bundleinfo/Random.java'
}
archiveName 'api-javadoc-sources.zip'
destinationDir(file('image'))
}
I want to retain the directory structure in zip as
api-javadoc-sources.zip
-- core-module
-- /src/mypackage/
-- subpackage/.java
-- anotherpackage/.java
-- lib-module
-- /src/mypackage/
-- bundleinfo/*.java
But, My implementation just putting all `*.java` in root of `.zip` , how can I retain the directory structure here inside zip?
</details>
# 答案1
**得分**: 2
`fileTree()` 方法不会保留您从中提取的目录层次结构。您需要在复制规范中指定 `into` 目录,以表明您希望将匹配的文件放置在 zip 文件内部的某个目录中。
以下任务应该实现您想要的效果:
```groovy
task ('apiJavaDocSources', type: Zip) {
// 在此目录中搜索文件
from('core-module/src/mypackage') {
// 表示所有匹配的文件应该嵌套在 zip 文件内的此目录中
into 'core-module/src/mypackage'
// 匹配文件模式
include 'subpackage/*Exception.java'
include 'subpackage/package-info.java'
include 'anotherpackage/*.java'
}
from('lib-module/src/mypackage') {
into 'lib-module/src/mypackage'
include 'bundleinfo/*Dummy.java'
include 'bundleinfo/Random.java'
}
archiveName 'api-javadoc-sources.zip'
destinationDir(file('image'))
}
英文:
The fileTree()
method will not preserve the directory hierarchy you are sourcing from. You need to specify the into
directory in your copy specifications to specify that you want the matched files to be placed in a directory inside of the zip.
The following task should accomplish what you want:
task ('apiJavaDocSources', type: Zip) {
// search for files in this directory
from('core-module/src/mypackage') {
// indicates that all matches files should be nested in this directory inside of the zip
into 'core-module/src/mypackage'
// match file patterns
include 'subpackage/*Exception.java'
include 'subpackage/package-info.java'
include 'anotherpackage/*.java'
}
from('lib-module/src/mypackage') {
into 'lib-module/src/mypackage'
include 'bundleinfo/*Dummy.java'
include 'bundleinfo/Random.java'
}
archiveName 'api-javadoc-sources.zip'
destinationDir(file('image'))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论