如何使用 Bash 从 JAR 读取 MANIFEST.MF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7066063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read MANIFEST.MF file from JAR using Bash
提问by Roman
I need to read MANIFEST.MF maven manifest file from "some.jar" using bash
我需要使用 bash 从“some.jar”中读取 MANIFEST.MF maven manifest 文件
回答by miku
$ unzip -q -c myarchive.jar META-INF/MANIFEST.MF
-q
will suppress verbose output from the unzip program-c
will extract to stdout
-q
将抑制解压缩程序的详细输出-c
将提取到标准输出
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
代替-q -c
.
-pextract 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).
-p 将文件提取到管道 (stdout)。除了文件数据之外什么都不会发送到标准输出,并且文件总是以二进制格式提取,就像它们被存储一样(没有转换)。
回答by Bobby Powers
use unzip
:
使用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:
它将安静地 ( -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
(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.
(这将删除所有不包含 stringMain-Class
的行,然后将行拆分为:
,仅保留第二个字段,即类名)。当然,可以$JARFILE_PATH
适当地定义或替换$JARFILE_PATH
为您感兴趣的 jarfile 的路径。
回答by Zsolt Botykai
Depending on your distribution, install the unzip
package. Then simply issue
根据您的发行版,安装unzip
软件包。然后简单地发出
unzip -p YOUR_FILE.jar META-INF/MANIFEST.MF
This will dump the contents to STDOUT.
这会将内容转储到 STDOUT。
HTH
HTH
回答by rlovtang
$ tar xfO some.jar META-INF/MANIFEST.MF
$ tar xfO some.jar META-INF/MANIFEST.MF
x
extracts and O
redirects to stdout.
x
提取并O
重定向到标准输出。
Note: Seem to work only in bsdtar, not GNU tar.
注意:似乎只能在 bsdtar 中工作,而不能在 GNU tar 中工作。
回答by Justin Clayton
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.
其他人已经发布了关于使用 unzip -p 和管道到 grep 或 awk 或任何你需要的东西。虽然这适用于大多数情况,但值得注意的是,由于 MANIFEST.MF 的每行 72 个字符的限制,您可能正在搜索其值跨多行拆分的键,因此很难解析。我很想看到一个 CLI 工具,它实际上可以从文件中提取一个渲染值。
http://delaltctrl.blogspot.com/2009/11/manifestmf-apparently-you-are-just.html
http://delaltctrl.blogspot.com/2009/11/manifestmf-apparently-you-are-just.html
回答by ctrueden
The following Groovy script uses Java's API to parse the manifest, avoiding issues with the manifest format's weird line breaking:
以下 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") }
}
Pass JAR files as arguments:
将 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
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