我关注这个Reading my own Jar's Manifest以编程方式获取 list 条目的指南。
我正在使用 maven- assembly-plugin 构建 jar 文件。 list 文件位于 jar 文件内的 META-INF/MANIFEST.MF
中。
问题是无论如何我都会得到空条目。我尝试使用JarFile加载,使用ClassLoaderResources没有效果。
但是,如果在 pom 文件中添加额外的条目,那么如果这些条目位于单独的部分(用额外的换行符分隔),我可以读取这些条目。
如果我将条目添加到主要属性部分,那么它们是不可见的。
出于某种安全原因,是否有可能禁用以编程方式读取 manifest.mf 文件?
最佳答案
我不知道为什么在没有额外依赖项的情况下找不到 list 条目,但从 list 读取时不应该有任何问题。我这样做是这样的:
private static final String MANIFEST_FILE_KEY = "META-INF/MANIFEST.MF";
......
try (JarFile jar = new JarFile(URLDecoder.decode(jarFileURL.getPath(), "UTF-8"))) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().equals(MANIFEST_FILE_KEY)){
//found our manifest
Manifest manifest = new Manifest(jar.getInputStream(entry));
Attributes attributes = manifest.getMainAttributes();
for (Entry<Object, Object> key : attributes.entrySet()) {
//do something with the key values
}
break;
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
关于java - 如何从与jar捆绑的manifest.mf文件中获取条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25488435/