in hawtio-util/src/main/java/io/hawt/util/Objects.java [63:119]
public static String getVersion(Class<?> aClass, String groupId, String artifactId) {
String version = null;
// lets try find the maven property - as the Java API rarely works :)
InputStream is = null;
String fileName = "/META-INF/maven/" +
groupId + "/" + artifactId +
"/pom.properties";
// try to load from maven properties first
try {
Properties p = new Properties();
is = aClass.getResourceAsStream(fileName);
if (is != null) {
p.load(is);
version = p.getProperty("version", "");
}
} catch (Exception e) {
// ignore
} finally {
if (is != null) {
IOHelper.close(is, fileName, LOG);
}
}
if (version == null) {
Package aPackage = aClass.getPackage();
if (aPackage != null) {
version = aPackage.getImplementationVersion();
if (Strings.isBlank(version)) {
version = aPackage.getSpecificationVersion();
}
}
}
if (version == null) {
Enumeration<URL> resources = null;
try {
resources = aClass.getClassLoader().getResources("META-INF/MANIFEST.MF");
} catch (IOException e) {
// ignore
}
if (resources != null) {
String expectedBundleName = groupId + "." + artifactId;
while (resources.hasMoreElements()) {
try {
Manifest manifest = new Manifest(resources.nextElement().openStream());
Attributes attributes = manifest.getMainAttributes();
String bundleName = attributes.getValue("Bundle-SymbolicName");
if (Objects.equals(expectedBundleName, bundleName)) {
version = attributes.getValue("Implementation-Version");
if (Strings.isNotBlank(version)) break;
}
} catch (IOException e) {
// ignore
}
}
}
}
return version;
}