Skip to content

Commit

Permalink
read version info from from MANIFEST.MF using different strategy (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
chewiebug committed Feb 15, 2016
1 parent 37376ba commit babffd3
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/main/java/com/tagtraum/perf/gcviewer/util/BuildInfoReader.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.tagtraum.perf.gcviewer.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

/**
* Small helper class to provide current version of GCViewer.
Expand All @@ -13,7 +14,7 @@
*/
public class BuildInfoReader {

private final static String FILE_NAME = "META-INF/MANIFEST.MF";
private final static String FILE_NAME = "/META-INF/MANIFEST.MF";
private final static String BUILD_VERSION = "Implementation-Version";
private final static String BUILD_TIMESTAMP = "Implementation-Date";

Expand All @@ -25,14 +26,11 @@ public class BuildInfoReader {
*/
private static String readPropertyValue(String propertyName) {
String propertyValue = "n/a";
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(FILE_NAME)) {
if (in != null) {
Properties props = new Properties();
props.load(in);
propertyValue = props.getProperty(propertyName);
if (propertyValue == null || propertyValue.length() == 0) {
propertyValue = "n/a";
}
try {
Attributes attributes = getAttributes();
propertyValue = attributes.getValue(propertyName);
if (propertyValue == null || propertyValue.length() == 0) {
propertyValue = "n/a";
}
}
catch (IOException e) {
Expand All @@ -42,6 +40,20 @@ private static String readPropertyValue(String propertyName) {
return propertyValue;
}

private static Attributes getAttributes() throws IOException {
Class clazz = BuildInfoReader.class;
String className = clazz.getSimpleName() + ".class";
String classPath = clazz.getResource(className).toString();
if (!classPath.startsWith("jar")) {
// Class not from JAR
return new Attributes(0);
}
String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + FILE_NAME;
Manifest manifest = null;
manifest = new Manifest(new URL(manifestPath).openStream());
return manifest.getMainAttributes();
}

/**
* Read version from properties file in classpath if it can be found.
*
Expand Down

0 comments on commit babffd3

Please sign in to comment.