如何使用 Bash 从 JAR 读取 MANIFEST.MF 文件

发布于 2024-11-29 09:32:09 字数 54 浏览 2 评论 0原文

我需要使用 bash 从“some.jar”读取 MANIFEST.MF maven 清单文件

I need to read MANIFEST.MF maven manifest file from "some.jar" using bash

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

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

发布评论

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

评论(6

感性不性感 2024-12-06 09:32:09
$ unzip -q -c myarchive.jar META-INF/MANIFEST.MF
  • -q 将抑制解压缩程序的详细输出
  • -c 将提取到 stdout

示例:

$ unzip -q -c commons-lang-2.4.jar META-INF/MANIFEST.MF

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0_13-119 (Apple Inc.)
Package: org.apache.commons.lang
Extension-Name: commons-lang
Specification-Version: 2.4
Specification-Vendor: Apache Software Foundation
Specification-Title: Commons Lang
Implementation-Version: 2.4
Implementation-Vendor: Apache Software Foundation
Implementation-Title: Commons Lang
Implementation-Vendor-Id: org.apache
X-Compile-Source-JDK: 1.3
X-Compile-Target-JDK: 1.2

或者您可以使用 -p 而不是 -q -c

-p 将文件提取到管道(stdout)。除了文件数据之外什么都不会发送到 stdout,并且文件始终以二进制格式提取,就像存储时一样(没有转换)。

$ unzip -q -c myarchive.jar META-INF/MANIFEST.MF
  • -q will suppress verbose output from the unzip program
  • -c will extract to stdout

Example:

$ unzip -q -c commons-lang-2.4.jar META-INF/MANIFEST.MF

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0_13-119 (Apple Inc.)
Package: org.apache.commons.lang
Extension-Name: commons-lang
Specification-Version: 2.4
Specification-Vendor: Apache Software Foundation
Specification-Title: Commons Lang
Implementation-Version: 2.4
Implementation-Vendor: Apache Software Foundation
Implementation-Title: Commons Lang
Implementation-Vendor-Id: org.apache
X-Compile-Source-JDK: 1.3
X-Compile-Target-JDK: 1.2

Alternatively you can use -p instead of -q -c.

-p extract files to pipe (stdout). Nothing but the file data is sent to stdout, and the files are always extracted in binary format, just as they are stored (no conversions).

北风几吹夏 2024-12-06 09:32:09

使用unzip

$ unzip -q -c $JARFILE_PATH META-INF/MANIFEST.MF

它将悄悄地(-q)从jarfile(使用zip格式压缩)读取路径META-INF/MANIFEST.MF到stdout(<代码>-c)。然后,您可以将输出通过管道传输到其他命令来回答诸如“这个 jar 的主类是什么:”之类的问题:(

$ unzip -q -c $JARFILE_PATH META-INF/MANIFEST.MF | grep 'Main-Class' | cut -d ':' -f 2

这会删除所有不包含字符串 Main-Class 的行,然后拆分该行在 : 处,仅保留第二个字段(类名)。当然,可以适当地定义 $JARFILE_PATH 或将 $JARFILE_PATH 替换为您感兴趣的 jarfile 的路径。

use unzip:

$ unzip -q -c $JARFILE_PATH META-INF/MANIFEST.MF

that will quietly (-q) read the path META-INF/MANIFEST.MF from the jarfile (which is compressed using the zip format) to stdout (-c). You can then pipe the output to other command to answer questions like 'what is the main class for this jar:

$ unzip -q -c $JARFILE_PATH META-INF/MANIFEST.MF | grep 'Main-Class' | cut -d ':' -f 2

(this removes all lines which don't contain the string Main-Class, then splits the line at :, keeping only the second field, the class name). Of course, either define $JARFILE_PATH appropriately or replace $JARFILE_PATH with the path to a jarfile you're interested in.

春花秋月 2024-12-06 09:32:09

根据您的发行版,安装 unzip 软件包。然后只需发出

unzip -p YOUR_FILE.jar META-INF/MANIFEST.MF

This 会将内容转储到 STDOUT。

华泰

Depending on your distribution, install the unzip package. Then simply issue

unzip -p YOUR_FILE.jar META-INF/MANIFEST.MF

This will dump the contents to STDOUT.

HTH

挽容 2024-12-06 09:32:09

$ tar xfO some.jar META-INF/MANIFEST.MF

x 提取并 O 重定向到 stdout。

注意:似乎仅适用于 bsdtar,不适用于 GNU tar。

$ tar xfO some.jar META-INF/MANIFEST.MF

x extracts and O redirects to stdout.

Note: Seem to work only in bsdtar, not GNU tar.

谎言月老 2024-12-06 09:32:09

其他人已经发布了有关使用 unzip -p 和管道到 grep 或 awk 或任何您需要的内容的文章。虽然这适用于大多数情况,但值得注意的是,由于 MANIFEST.MF 每行 72 个字符的限制,您可能会查找其值分散在多行中的键,因此很难解析。我很想看到一个 CLI 工具能够真正从文件中提取渲染值。

http://delaltctrl.blogspot.com/2009/11 /manifestmf-apparently-you-are-just.html

Others have been posting about using unzip -p and piping to grep or awk or whatever you need. While that works for most cases, it's worth noting that because of the 72 characters-per-line limit of MANIFEST.MF, you may be grepping for keys whose values are split across multiple lines and will therefore be very difficult to parse. I'd love to see a CLI tool that can actually pull a rendered value out of the file.

http://delaltctrl.blogspot.com/2009/11/manifestmf-apparently-you-are-just.html

坏尐絯 2024-12-06 09:32:09

以下 Groovy 脚本使用 Java 的 API 来解析清单,避免了清单格式奇怪的换行问题:

#!/usr/bin/env groovy
for (arg in args) {
  println("[$arg]")
  jarPath = new java.io.File(arg).getAbsolutePath()
  jarURL = new java.net.URL("jar:file:" + jarPath + "!/")
  m = jarURL.openConnection().getManifest()
  m.getMainAttributes().each { k, v -> println("$k = $v") }
}

将 JAR 文件作为参数传递:

$ groovy manifest.groovy ~/.m2/repository/junit/junit/4.13/junit-4.13.jar
[/Users/curtis/.m2/repository/junit/junit/4.13/junit-4.13.jar]
Implementation-Title = JUnit
Automatic-Module-Name = junit
Implementation-Version = 4.13
Archiver-Version = Plexus Archiver
Built-By = marc
Implementation-Vendor-Id = junit
Build-Jdk = 1.6.0_65
Created-By = Apache Maven 3.1.1
Implementation-URL = http://junit.org
Manifest-Version = 1.0
Implementation-Vendor = JUnit

或者如果您迫切需要一行代码:

groovy -e 'new java.net.URL("jar:file:" + new java.io.File(args[0]).getAbsolutePath() + "!/").openConnection().getManifest().getMainAttributes().each { k, v -> println("$k = $v") }' ~/.m2/repository/junit/junit/4.13/junit-4.13.jar

The following Groovy script uses Java's API to parse the manifest, avoiding issues with the manifest format's weird line breaking:

#!/usr/bin/env groovy
for (arg in args) {
  println("[$arg]")
  jarPath = new java.io.File(arg).getAbsolutePath()
  jarURL = new java.net.URL("jar:file:" + jarPath + "!/")
  m = jarURL.openConnection().getManifest()
  m.getMainAttributes().each { k, v -> println("$k = $v") }
}

Pass JAR files as arguments:

$ groovy manifest.groovy ~/.m2/repository/junit/junit/4.13/junit-4.13.jar
[/Users/curtis/.m2/repository/junit/junit/4.13/junit-4.13.jar]
Implementation-Title = JUnit
Automatic-Module-Name = junit
Implementation-Version = 4.13
Archiver-Version = Plexus Archiver
Built-By = marc
Implementation-Vendor-Id = junit
Build-Jdk = 1.6.0_65
Created-By = Apache Maven 3.1.1
Implementation-URL = http://junit.org
Manifest-Version = 1.0
Implementation-Vendor = JUnit

Or if you're desperate for a one-liner:

groovy -e 'new java.net.URL("jar:file:" + new java.io.File(args[0]).getAbsolutePath() + "!/").openConnection().getManifest().getMainAttributes().each { k, v -> println("$k = $v") }' ~/.m2/repository/junit/junit/4.13/junit-4.13.jar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文