MANIFEST.MF属性返回null



我试图从MANIFEST.MF文件中获取一些属性,但它们返回null。

歧管

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver 
Built-By: jenkins 
Build-Time: 2016-04-09T18:02:46Z Class-Path: spigot-1.8-R0.1-SNAPSHOT.jar Created-By: Apache Maven 3.3.9 
Build-Jdk: 1.8.0_77
Name: Build Details 
Implementation-SCM-Revision: SomeCommit 
Implementation-Build-Number: SomeBuildNumber 
Implementation-Title: SomeTitle 
Implementation-Version: 4.0.0-SNAPSHOT

代码(此代码不是使用的确切代码,但几乎相同)

首次尝试

 public void doStuff() {
    String version;
    InputStream stream = Main.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF");
    Properties properties = new Properties();
    if (stream != null) {
        try {
            properties.load(stream);
            version = properties.getProperty("Implementation-SCM-Revision"); //This is null 
        } catch (IOException ex) {
            //TODO handle exception
        }
    }

第二次尝试

public void doStuff() {
    Enumeration resEnum;
    try {
        resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
        while (resEnum.hasMoreElements()) {
            try {
                URL url = (URL)resEnum.nextElement();
                InputStream is = url.openStream();
                if (is != null) {
                    Manifest manifest = new Manifest(is);
                    Attributes mainAttribs = manifest.getMainAttributes();
                    String version = mainAttribs.getValue("Implementation-SCM-Revision"); //This is null
                }
            }
            catch (Exception e) {
            }
        }
    } catch (IOException e1) {
    }
}

问题是,当Attributes#getMainAttributes()中的所有键都打印出来时,它会打印[Archiver-Version, Implementation-Title, Sealed, Specification-Version, Implementation-Version, Created-By, Manifest-Version, Build-Jdk, Specification-Vendor, Implementation-Vendor, Ant-Version, Specification-Title, Built-By, Main-Class],而不包括Implementation-SCM-Revision之类的东西。这就是我想要得到的。

没有生成stacktrace,它只是在打印Implementation-SCM-Revision属性时在控制台中打印null

Oracle成功地破坏了Manifest类,尽管它很难:
JDK-8201636尝试读取清单文件属性时为空值

另请参阅MANIFEST实体的类似错误:
JDK-8031748澄清jar文件中的jar条目顺序:

从一开始,jar文件就有一个"未记录"的假设MANIFEST.MF文件和签名相关文件(块和SF)应该出现在开头(除了目录条目,META-INF/)。

当前的答案是错误的。虽然它提到的错误报告并非无关,但问题中提供的代码中存在明显错误。因此,它并没有提供错误的信息,但它表明了一个错误的结论。


属性Implementation-SCM-Revision不是主属性,而是清单条目Build Details的属性。

简化后,清单具有以下形式

# fixed
Manifest-Version: 1.0
# Main Attributes
Implementation-Version: 1.0
# 'Name:' marks the beginning of an entry
Name: com.example.Foo
# all following lines belong to the previously started entry
# this line therefore applies to com.example.Foo
Checksum: 76652367792347

因此,要检索Implementation-SCM-Revision的值,必须执行

manifest.getAttributes("Build Details").getValue("Implementation-SCM-Revision");

反过来,为了通过manifest.getMainAttributes()检索Implementation-SCM-Revision,它必须高于Name:

Manifest-Version: 1.0
# other main attributes
Implementation-SCM-Revision: SomeCommit
Name: Build Details
# other entry-related attributes

最新更新