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");

  1. #1 by sreejith on 2012/07/26 - 14:49

    Hi ,

    I tried the above java code to get project version from pom.properties,but failed.Its returning null,since the path specified to pom.properties streams null.I tried with many alternatives but i could not read that pom.proerties from META-INF.Is there anything i need to do in the pom.xml to access this way of getting version.

    I used another approach like a custom property file in resources folder and i can read that custrom property file from the jar but could not get the pom.proerties.

    Thank you

    • #2 by Thilo on 2012/07/27 - 10:55

      Hi,

      without any details about the jar or project you apply the code above, I can’t help you.
      Each jar can be unzipped like a common archive file. Have a look inside the folder ‘META-INF’ and check if the ‘pom.properties’ is inside the expected folder. If not, the jar doesn’t follows the naming convention of maven, – and so the code above fails.

      Hope that helps,
      kind regards
      Thilo

      • #3 by sreejith on 2012/07/27 - 11:29

        Hi Thilo,

        Thanks for the reply.Yes the jar as well as the extracted zip contains the file pom.properties under META-INF.

        I am trying to read the version from pom.properties file from a class with in the project itself,after “mvn clean install”

        I would like to know Is there any parameters that i need to add in pom.xml before doing this ?
        I think i am missing in that part.

        I can read a custom file from the jar which is placed in the root of jar itself. But can not get the META-INF/../../pom.properties

        Thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: