如何使用 Gradle 的 Manifest API 将“MANIFEST.MF”文件插入 zip 文件的根目录

发布于 2024-12-29 00:03:18 字数 1257 浏览 2 评论 0原文

我们使用 java 插件的 manifest 属性将 MANIFEST.MF 文件写入我们的 jar 工件。

我们还使用 gradle 构建 GWT 项目,并且我们为这些项目定义的输出是 zip。我想在该 zip 文件的根目录中包含一个 MANIFEST.MF 文件。

我尝试使用 type: Jar 的任务,这样我就可以使用它的 manifest 属性,但问题当然是清单文件被写入 META-INF/MANIFEST.MF,我不想要。原因是我们将存档解压缩到主应用程序中,并且我需要能够在运行时引用 MANIFEST.MF 文件以达到我自己的邪恶目的。

所以现在档案看起来像这样:

/gwtdirectory/
/gwtdirectory/file1
/gwtdirectory/file2
/gwtdirectory/...
/gwtdirectory/filen

我需要它像这样:

/gwtdirectory/
/gwtdirectory/MANIFEST.MF
/gwtdirectory/file1
/gwtdirectory/file2
/gwtdirectory/...
/gwtdirectory/filen

我已经成功地让它看起来像:

/gwtdirectory/
/gwtdirectory/META-INF/MANIFEST.MF
/gwtdirectory/file1
/gwtdirectory/file2
/gwtdirectory/...
/gwtdirectory/filen

通过这样的定义:

task pack(type: Jar){
  manifest {
    attributes(...)
  }
  extension = 'zip'
  from gwt.destinationDir
}

writeTo 方法看起来很有前途,除了我找不到该接口的实现我可以使用它,但我试图避免自己编写。

想法?

We use the manifest attribute of the java plugin to write MANIFEST.MF files to our jar artifacts.

We also use gradle to build GWT projects and the output we've defined for those projects is a zip. I'd like to include a MANIFEST.MF file in the root of that zip file.

I've tried using a task of type: Jar so I can use its manifest property but the problem, of course, is that the manifest file is written into META-INF/MANIFEST.MF, which I don't want. The reason is that we unzip the archive into the main app and I need to be able to reference the MANIFEST.MF file at runtime for my own nefarious purposes.

So right now the archive looks like this:

/gwtdirectory/
/gwtdirectory/file1
/gwtdirectory/file2
/gwtdirectory/...
/gwtdirectory/filen

And I need it took like this:

/gwtdirectory/
/gwtdirectory/MANIFEST.MF
/gwtdirectory/file1
/gwtdirectory/file2
/gwtdirectory/...
/gwtdirectory/filen

I've already successfully gotten it to look like:

/gwtdirectory/
/gwtdirectory/META-INF/MANIFEST.MF
/gwtdirectory/file1
/gwtdirectory/file2
/gwtdirectory/...
/gwtdirectory/filen

Through a definition like:

task pack(type: Jar){
  manifest {
    attributes(...)
  }
  extension = 'zip'
  from gwt.destinationDir
}

The writeTo method looks mighty promising, except I can't find an implementation of that interface that I can use and I'm trying to avoid writing my own.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2025-01-05 00:03:18

查看 Jar任务,看起来manifest文件的位置是不可配置的。

你可能想使用gradle自己的 DefaultManifest 实现来创建清单,然后将此文件与 from 一起包含在您的 zip 文件中。

你可以试试这个:

import org.gradle.api.java.archives.internal.DefaultManifest
import org.gradle.api.internal.file.IdentityFileResolver

File manifestFile = file('build/myAwesomeManifest.mf')

task generateManifest << {
   manifest = new DefaultManifest(new IdentityFileResolver())

   // add some attributes
   manifest.attributes(["attr1":"value1", "attr2":"value2"])

   // write it to a file
   manifest.writeTo(manifestFile)
}

Looking at the Jar task, it does not look like the location of the manifest file is configurable.

You may want to use gradle's own DefaultManifest implementation to create a manifest and then include this file with from in your zip file.

You can try this:

import org.gradle.api.java.archives.internal.DefaultManifest
import org.gradle.api.internal.file.IdentityFileResolver

File manifestFile = file('build/myAwesomeManifest.mf')

task generateManifest << {
   manifest = new DefaultManifest(new IdentityFileResolver())

   // add some attributes
   manifest.attributes(["attr1":"value1", "attr2":"value2"])

   // write it to a file
   manifest.writeTo(manifestFile)
}
墟烟 2025-01-05 00:03:18

我认为在 本章末尾正是您要寻找的内容< /a> 来自 Gradle 手册。

jar.manifest.writeTo("$buildDir/mymanifest.mf")

I think there is exactly what you are looking for at the end of this chapter from Gradle manual.

jar.manifest.writeTo("$buildDir/mymanifest.mf")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文