java - MANIFEST.MF 属性返回 null

标签 java attributes manifest

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

list

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。这就是我想要得到的。

没有生成堆栈跟踪,它只是在打印 Implementation-SCM-Revision 属性时在控制台打印 null

最佳答案

Oracle 设法打破了 Manifest 类,尽管这很困难:
JDK-8201636 null value when trying to read manifest file attributes

另请参阅 MANIFEST entires 的类似错误:
JDK-8031748 Clarify jar entry orders in a jar file :

From the beginning, the jar file has an “undocumented” assumption that the MANIFEST.MF file and signature-related files (block and SF) should appear at the beginning (except for directory entries, say, META-INF/).

关于java - MANIFEST.MF 属性返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36529228/

相关文章:

java - 有没有办法多次迭代 HttpServletRequest.getAttributeNames() ?

python - 是什么导致了这个Python异常?

c++ - 将 list 文件移动到 dll?

java - 使用 java 7u45 启动已签名的小程序时出现的问题

java - 日志消息从何而来

java - 如何使用 ThreadPoolExecutor 和自定义任务实现 PriorityBlockingQueue

methods - Proxy.new (Raku) 中的属性值变为 "Method"

java - 解析 jar 中 manifest.mf 文件条目的正确方法是什么?

java - 线程 "main"java.lang.AbstractMethodError SpringBoot 中的异常

Java版本控制