许多Java jar中包含的manifest.mf包含看起来很像电子邮件标题的标头。请参阅示例 [*]
我想要一些可以将这种格式解析为键值对的东西:
Map<String, String> manifest = <mystery-parse-function>(new File("manifest.mf"));
我已经在谷歌上搜索了一下"parse manifest.mf"manifest.mf 格式"等,我找到了大量关于标头含义的信息(例如在 OSGI 捆绑包、标准 Java jar 等中),但这不是我想要的。
查看一些示例 manifest.mf 文件,我可能会实现一些东西来解析它(对格式进行逆向工程),但我不知道我的实现是否真的正确。所以我也不是在寻找其他人的快速组合解析函数,因为它遇到了同样的问题)。
对我的问题的一个很好的回答可以指出格式的规范(这样我就可以编写自己的正确解析函数)。最好的答案是指向一个已经具有正确实现的现有开源库。
[*] = https://gist.github.com/kdvolder/6625725
MANIFEST。可以使用清单类读取 MF 文件:
Manifest manifest = new Manifest(new FileInputStream(new File("MANIFEST.MF")));
然后你可以通过做
Map<String, Attributes> entries = manifest.getEntries();
以及所有主要属性通过做
Attributes attr = manifest.getMainAttributes();
一个工作示例
我的MANIFEST.MF
文件是这样的:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
我的代码:
Manifest manifest = new Manifest(new FileInputStream(new File("MANIFEST.MF")));
Attributes attr = manifest.getMainAttributes();
System.out.println(attr.getValue("Manifest-Version"));
System.out.println(attr.getValue("X-COMMENT"));
输出:
1.0
Main-Class will be added automatically by build