我需要使用 bash 从“some.jar”中读取 MANIFEST.MF Maven 清单文件
6 回答
$ unzip -q -c myarchive.jar META-INF/MANIFEST.MF
-q
将抑制解压缩程序的详细输出-c
将提取到标准输出
例子:
$ 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提取文件到管道(标准输出)。只有文件数据被发送到标准输出,并且文件总是以二进制格式提取,就像它们被存储一样(没有转换)。
使用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 的路径。
根据您的发行版,安装unzip
软件包。然后简单地发出
unzip -p YOUR_FILE.jar META-INF/MANIFEST.MF
这会将内容转储到 STDOUT。
高温高压
$ tar xfO some.jar META-INF/MANIFEST.MF
x
提取并O
重定向到标准输出。
注意:似乎只在 bsdtar 中有效,在 GNU tar 中无效。
其他人一直在发布有关使用 unzip -p 和管道到 grep 或 awk 或任何您需要的东西的帖子。虽然这适用于大多数情况,但值得注意的是,由于 MANIFEST.MF 的每行 72 个字符的限制,您可能正在寻找其值被拆分为多行的键,因此很难解析。我很想看到一个 CLI 工具,它实际上可以从文件中提取渲染值。
http://delaltctrl.blogspot.com/2009/11/manifestmf-apparently-you-are-just.html
以下 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