2

I want to get to read from the Manifest of my Jar file to get the SVNVersion info from there and show it in the UI. I work on a multi-module project that includes a client, a server running on tomcat, some modules of the client and its dependencies.

I wrote the code to access the current thread's manifest and get it's attributes.

Manifest mf = new Manifest();

    mf.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"));

    Attributes atts = mf.getMainAttributes();
    System.out.println(atts.values());
    System.out.println("Version: " + atts.getClass());
    System.out.println("Revision: " + atts.get("SCM-Revision"));
    String scm = (String) atts.get("SCM-Revision");
    String revision = (String) atts.get("Revision-Number");
    return revision + scm;
}

The only thing is that it gets Null values returned because it get's the manifest of a dependency from the local repository that doesn't have the information i need inside of it.

Is there a solution to my problem? to specify a module from the project in a way so it know's to get IT's jar's Manifest and not the one from the dependency? Thank you!

SHPstr
  • 91
  • 9

2 Answers2

3

(I deleted my previous answer because it was buggy and unaccurate)

This is the procedure I used to have to get the proper manifest from any class in the classpath: It consists of getting it through a specific URL:

private static URL getManifestUrlForClass(Class<?> cl)
    throws MalformedURLException
{
    URL url=cl.getResource(cl.getSimpleName() + ".class");
    String s=url.toString();
    return new URL(s.substring(0, s.length() - (cl.getName() + ".class").length()) + "META-INF/MANIFEST.MF");

To use it from your code, just replace the first line:

mf.read(getManifestUrlForClass(MyClass.class).openStream());
Little Santi
  • 8,563
  • 2
  • 18
  • 46
  • Thanks! I'll try this on Monday. – SHPstr May 19 '17 at 16:15
  • Okay, I've tried this and it brought me closer to something that I need. It gives me the location of the current's module's class. From here i can navigate and get the Manifest. I just have to fiddle with it more. It's not the final answer but it helps a lot. Thank you! – SHPstr May 22 '17 at 11:12
  • Actually, after trying it on the non-dev environment it works. Thanks again! – SHPstr May 22 '17 at 13:17
0

You get a null because manifests are disabled in maven by default and what you get are values from the Jdk (jar containing the Thread-classfile). You either read

this.getClass().getResourceAsStream("/META-INF/.../pom.properties")

or add the capability to create a manifest (depends on the modul packaging you have to pick the correct build-plugin for that).

Warning: "/META-INF/.../pom.properties" might have the same path in different Jars. Choose the Class first to determin what pom.properties you like to read.

Grim
  • 1,938
  • 10
  • 56
  • 123
  • I get null because the values that i am searching for inside the manifest do not exist, because i get the manifest file of a dependency instead of the client. – SHPstr May 19 '17 at 13:41
  • @SHPstr Thats what I said. – Grim May 19 '17 at 13:43