Get Implementation-Version from WAR

Viewed 464

I have a web service running inside a Wildfly 8.1.0 server.

The web service's WAR file contains inside the MANIFEST.MF file this line:

Implementation-Version: 1.0.32

I want to read this from one of the API's endpoints. I tried several things found on the internet:

getClass().getPackage().getImplementationVersion()

Returns null. Also with jcabi manifests library:

Manifests.read("Implementation-Version");

Returns 8.1.0.Final. This makes me think it actually reads Wildfly's MANIFEST file.

How can I read what is inside my WAR's MANIFEST file?

1 Answers

This is what Spring Boot did to get back the implementation version from a Jar.

Because a Web ARchive can be thought of in the same way as a Java ARchive (and yes, that capitalization is intentional), one believes that using the JarFile class to read the manifest would be enough.

Additionally, because I don't know where in your project lifecycle this WAR would be, I can't prescribe anything different than the source code that Spring had (which is referenced here in full). You have to read the file in from some place, and then you can get the manifest from that.

private static String getImplementationVersion(JarFile jarFile) throws IOException {
    return jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
}
Related