Posts Tagged jar version info

Getting version info of jars.

In common cases (the jar contains the file MANIFEST.MF with all required standard infos) you can grab desired infos with:

String title = someClass.getPackage().getImplementationTitle();
String version = someClass.getPackage().getImplementationVersion();

But if you have a library that contains different sublibraries you run into problems. Typically those libraries are build with maven. The desired infos are stored in several pom.properties files (one for each sub project).
Here is a code-snippet to read the infos form a pom-properties file of a subproject. You just have to know the group-id and the artifact-id:

/**
 * LGPL
 */
Map<String, String> getInfoFromMetaPom(final String groupId, final String artifactId) {
 String pomPath = String.format("META-INF/maven/%s/%s/pom.properties", groupId, artifactId);
 Map<String, String> info = new HashMap<String, String>(2);
 info.put("title", "unknown");
 info.put("version", "n/n");

 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(pomPath);
 if (in != null) {
   Properties properties = new Properties();
 try {
   in = new BufferedInputStream(in);
   properties.load(in);
 } catch (IOException ex) {
 }

 String title = properties.getProperty("artifactId");
 String version = properties.getProperty("version");
 if (title != null)
   info.put("title", title);
 if (version != null)
   info.put("version", version);
 }
 return info;
}

Map info = getInfoFromMetaPom("org.hibernate", "hibernate-core");
String title = info.get("title");
String version = info.get("version");
Advertisements

3 Comments