0

Technologies being used: Java 1.8 and Maven

I have a module (A) that is referencing another module (B) and I would like to get B's module version at runtime.

The code below is returning A's version rather than B's version at runtime.

I am also referencing this other question: reading MANIFEST.MF file from jar file using JAVA

Code in module (B) that returns its version:

public static String getVersion() {
    if (StringUtils.isBlank(version)) {
        Class<?> clazz = Runner.class;
        String className = clazz.getSimpleName() + ".class";
        String classPath = clazz.getResource(className).toString();
        if (!classPath.startsWith("jar")) {
            // Class not from JAR
            String relativePath = clazz.getName().replace('.', File.separatorChar) + ".class";
            String classFolder = classPath.substring(0, classPath.length() - relativePath.length() - 1);
            String manifestPath = classFolder + "/META-INF/MANIFEST.MF";
            //log.debug("manifestPath={}", manifestPath);
            version = readVersionFrom(manifestPath);
        } else {
            String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
            System.out.println(String.format("manifestPath={%s}", manifestPath));
            version = readVersionFrom(manifestPath);
        }
    }
    return version;
}

private static String readVersionFrom(String manifestPath) {
    Manifest manifest = null;
    try {
        manifest = new Manifest(new URL(manifestPath).openStream());
        Attributes attrs = manifest.getMainAttributes();

        String implementationVersion = attrs.getValue("Implementation-Version");
        implementationVersion = StringUtils.replace(implementationVersion, "-SNAPSHOT", "");
        //log.debug("Read Implementation-Version: {}", implementationVersion);

        String implementationBuild = attrs.getValue("Implementation-Build");
        //log.debug("Read Implementation-Build: {}", implementationBuild);

        String version = implementationVersion;
        if (StringUtils.isNotBlank(implementationBuild)) {
            version = StringUtils.join(new String[] { implementationVersion, implementationBuild }, '.');
        }
        return version;
    } catch (Exception e) {
        //log.error(e.getMessage(), e);
    }
    return StringUtils.EMPTY;
}

Code in module (A) to get version from module (B):

moduleB.getVersion()

Command I use to run module (A) packaged jar:

java -jar test.jar

jre247
  • 1,787
  • 5
  • 21
  • 28

1 Answers1

0
public static Manifest manifest(Class<?> clazz) throws IOException {
        URL resource = clazz.getResource("/" + clazz.getName().replace(".", "/") + ".class");
        System.out.println(resource);
        String resourceBase = resource.toString().replace(clazz.getName().replace(".", "/") + ".class", "");
        System.out.println(resourceBase);
        Enumeration<URL> resources = clazz.getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
            try {
                URL nextElement = resources.nextElement();
                if (nextElement.toString().equals(resourceBase + "META-INF/MANIFEST.MF")) {
                    System.out.println(nextElement);
                    Manifest manifest = new Manifest(nextElement.openStream());
                    return manifest;
                }
            } catch (IOException E) {
                //
            }
        }
        return null;
    }

System.out.println(manifest(T.class).getMainAttributes().getValue("Implementation-Version"));

don't forget to check if there even is a manifest, the it returns null