2010-09-23 74 views

回答

31

接下来的代码应帮助:

JarInputStream jarStream = new JarInputStream(stream); 
Manifest mf = jarStream.getManifest(); 

异常处理是留给你:)

28

你可以使用这样的事情:

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"); 
        if(version != null) { 
         return version; 
        } 
       } 
      } 
      catch (Exception e) { 
       // Silently ignore wrong manifests on classpath? 
      } 
     } 
    } catch (IOException e1) { 
     // Silently ignore wrong manifests on classpath? 
    } 
    return null; 
} 

要获得清单属性,你可以在变量迭代“mainAttribs”或直接检索您需要的属性,如果你知道的关键。

此代码遍历类路径上的每个jar并读取每个jar的清单。如果你知道罐子里的名字,你可能只想看看它是否包含()你有兴趣在罐子的名称对应的URL

+1

非常好,这通过所有清单。 – HankCa 2016-05-18 01:36:44

3

您可以使用一个工具类Manifestsjcabi-manifests

final String value = Manifests.read("My-Version"); 

类会发现所有在classpath中可用的个文件,并从其中一个文件中读取要查找的属性。此外,阅读这样的:http://www.yegor256.com/2014/07/03/how-to-read-manifest-mf.html

+0

这很棒,效果很好。很高兴它是一个图书馆。如果所有的清单都被读过,那么更好的办法是什么。我注意到Javadoc有一个'entry()',这可能是我的建议,但是这个方法并没有出现在我使用的版本中(1.1 - 这是MvnRepository上的最新版本)。答案http://stackoverflow.com/a/3777116/1019307有找到所有清单的解决方案。 – HankCa 2016-05-18 01:35:37

7

我根据从计算器一些想法实现的AppVersion类,在这里我只是共享整个类:

import java.io.File; 
import java.net.URL; 
import java.util.jar.Attributes; 
import java.util.jar.Manifest; 

import org.apache.commons.lang.StringUtils; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

public class AppVersion { 
    private static final Logger log = LoggerFactory.getLogger(AppVersion.class); 

    private static String version; 

    public static String get() { 
    if (StringUtils.isBlank(version)) { 
     Class<?> clazz = AppVersion.class; 
     String className = clazz.getSimpleName() + ".class"; 
     String classPath = clazz.getResource(className).toString(); 
     if (!classPath.startsWith("jar")) { 
     // Class not from JAR 
     String relativePath = clazz.getName().replace('.', File.separatorChar) + ".class"; 
     String classFolder = classPath.substring(0, classPath.length() - relativePath.length() - 1); 
     String manifestPath = classFolder + "/META-INF/MANIFEST.MF"; 
     log.debug("manifestPath={}", manifestPath); 
     version = readVersionFrom(manifestPath); 
     } else { 
     String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF"; 
     log.debug("manifestPath={}", manifestPath); 
     version = readVersionFrom(manifestPath); 
     } 
    } 
    return version; 
    } 

    private static String readVersionFrom(String manifestPath) { 
    Manifest manifest = null; 
    try { 
     manifest = new Manifest(new URL(manifestPath).openStream()); 
     Attributes attrs = manifest.getMainAttributes(); 

     String implementationVersion = attrs.getValue("Implementation-Version"); 
     implementationVersion = StringUtils.replace(implementationVersion, "-SNAPSHOT", ""); 
     log.debug("Read Implementation-Version: {}", implementationVersion); 

     String implementationBuild = attrs.getValue("Implementation-Build"); 
     log.debug("Read Implementation-Build: {}", implementationBuild); 

     String version = implementationVersion; 
     if (StringUtils.isNotBlank(implementationBuild)) { 
     version = StringUtils.join(new String[] { implementationVersion, implementationBuild }, '.'); 
     } 
     return version; 
    } catch (Exception e) { 
     log.error(e.getMessage(), e); 
    } 
    return StringUtils.EMPTY; 
    } 
} 

基本上,这个类可以从清单读版本信息的自己的JAR文件或其classes文件夹中的清单。希望它可以在不同的平台上工作,但我只在Mac OS X上测试过。

我希望这对其他人有用。

+1

唯一的问题是这个行包含:String manifestPath = StringUtils.join [...]使用File.separatorChar似乎在Windows下生成一个无效的URL ...可能最好使用“/”代替。 – 2013-07-24 11:38:19

+1

感谢您指出这一点。将尝试并修改它。 – 2013-07-24 13:38:25

+0

Waw,它的工作。谢谢! – 2016-12-05 18:04:13

22

我建议做以下几点:

Package aPackage = MyClassName.class.getPackage(); 
String implementationVersion = aPackage.getImplementationVersion(); 
String implementationVendor = aPackage.getImplementationVendor(); 

哪里MyClassName可以从你写你的应用程序的任何类。

+1

这是我需要的东西(阅读'Implementation-Version')。可悲的是,这对解开的类没有帮助,尽管这个清单是在'META-INF/MANIFEST.MF'中。 – 2016-03-02 10:27:41

+0

默认情况下这不起作用,除非您为maven-jar-plugin添加addDefaultImplementationEntries为true,以便它实际上包含版本信息。默认情况下,Manifest文件不包含任何内容。另外@Victor,尝试运行mvn包并查看里面是否有适当的信息。通常这些版本配置在maven-jar-plugin或maven-war-plugin中,所以未打包的类没有它。 – wonhee 2016-04-06 00:46:45

0

保持简单。一个JAR也是ZIP所以任何ZIP代码可用于读取MAINFEST.MF

public static String readManifest(String sourceJARFile) throws IOException 
{ 
    ZipFile zipFile = new ZipFile(sourceJARFile); 
    Enumeration entries = zipFile.entries(); 

    while (entries.hasMoreElements()) 
    { 
     ZipEntry zipEntry = (ZipEntry) entries.nextElement(); 
     if (zipEntry.getName().equals("META-INF/MANIFEST.MF")) 
     { 
      return toString(zipFile.getInputStream(zipEntry)); 
     } 
    } 

    throw new IllegalStateException("Manifest not found"); 
} 

private static String toString(InputStream inputStream) throws IOException 
{ 
    StringBuilder stringBuilder = new StringBuilder(); 
    try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) 
    { 
     String line; 
     while ((line = bufferedReader.readLine()) != null) 
     { 
      stringBuilder.append(line); 
      stringBuilder.append(System.lineSeparator()); 
     } 
    } 

    return stringBuilder.toString().trim() + System.lineSeparator(); 
} 

尽管灵活性,只是读取数据this答案是最好的。