Gradle Zip: 保留目录结构

huangapple go评论65阅读模式
英文:

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&#39;m creating the Zip of my java files as below but it doesn&#39;t retain the directory structure in the `.zip`

task ('apiJavaDocSources', type: Zip) {

from fileTree(&#39;core-module/src/mypackage/&#39;).matching {
    &#39;subpackage/*Exception.java&#39;
    &#39;subpackage/package-info.java&#39;
    &#39;anotherpackage/*.java&#39;
}

from fileTree(&#39;lib-module/src/mypackage/&#39;).matching {
    &#39;bundleinfo/*Dummy.java&#39;
    &#39;bundleinfo/Random.java&#39;
}
archiveName &#39;api-javadoc-sources.zip&#39;
destinationDir(file(&#39;image&#39;))

}


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 (&#39;apiJavaDocSources&#39;, type: Zip) {
	// search for files in this directory
	from(&#39;core-module/src/mypackage&#39;) {
		// indicates that all matches files should be nested in this directory inside of the zip
		into &#39;core-module/src/mypackage&#39;
		// match file patterns
        include &#39;subpackage/*Exception.java&#39;
        include &#39;subpackage/package-info.java&#39;
        include &#39;anotherpackage/*.java&#39;
	}

	from(&#39;lib-module/src/mypackage&#39;) {
		into &#39;lib-module/src/mypackage&#39;
        include &#39;bundleinfo/*Dummy.java&#39;
        include &#39;bundleinfo/Random.java&#39;
	}
	
    archiveName &#39;api-javadoc-sources.zip&#39;
    destinationDir(file(&#39;image&#39;))
}

huangapple
  • 本文由 发表于 2020年9月29日 20:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64119783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定