2017-07-21 24 views
0

相关:Get Maven artifact version at runtime获取Maven构件版本没有Java类

对比链接的问题,即依赖于getPackage() - 法一类的包,怎么能同一个结果来一个包来实现不包含java - 类,但只是一个支持库的Web资源?

+1

再次,取决于MANIFEST.MF与pom.properties。无论哪个人拥有他们,你都必须从你的班级路径中阅读他们,并通过他们看。 –

+0

研究更多关于阅读MANIFEST.MF(whick感激地被包装在.jar中)阻止我为我的问题找到正确的搜索查询,从而结束了下面的解决方案! – korgmatose

回答

0

使用在这里第二个答案找到了方法: reading MANIFEST.MF file from jar file using JAVA 并在Attributes增加一个条件 - 在标题检查在MANIFEST.MF呈现返回正确的版本。

public static String getManifestInfo() { 
     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-Version"); 
         String name = mainAttribs.getValue("Implementation-Title"); 
         if (version != null && name != null) { 
          if ("packagetitle".equals(name)) 
           return version; 
         } 
        } 
       } 
       catch (Exception e) { 
        // Silently ignore wrong manifests on classpath? 
       } 
      } 
     } catch (IOException e1) { 
      // Silently ignore wrong manifests on classpath? 
     } 
     return null; 
    } 

这一定能够更加有效地如果认识有关包本身的细节跳过使用流()迭代发,但至少这个工程按预期。