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



我需要阅读MANIFEST。使用bash

从"some.jar"创建MF maven清单文件
$ 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,并且文件总是以二进制格式提取,就像它们被存储一样(没有转换)。

use unzip:

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

将悄悄地(-q)读取路径META-INF/MANIFEST。MF从jarfile(使用zip格式压缩)到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替换为您感兴趣的jar文件的路径。

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

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

将内容转储到STDOUT。

HTH

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

x提取和O重定向到stdout。

注意:似乎只在bsdtar中工作,而不是GNU tar.

其他人已经发布了关于使用unzip -p和管道来grep或awk或任何您需要的内容。虽然这对大多数情况都有效,但值得注意的是,由于MANIFEST的每行限制为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

最新更新