Question

From a Java application I can log a string, using a custom logging framework, as follows:

logger.info("Version 1.2 of the application is currently running");

Therefore when a user sends me a log file I can easily see what version of the application they are running.

The problem with the code above is that the version is hardcoded in the string literal and someone needs to remember to update it for every release. It's fairly inevitable that updating this string could be forgotten for a release.

What I would like to do is have this string literal automatically updated based on one of the version numbers in the manifest file for the application's JAR:

Specification-Version: 1.2
Implementation-Version: 1.2.0.0

I don't mind if this happens at compile time or at runtime as long as the version number gets into the log file.

Was it helpful?

Solution

The standard way to get that information is SomeClass.class.getPackage().getImplementationVersion().

There's no need to do any parsing on your own.

OTHER TIPS

You can load the manifest at runtime using the following class

public class ManifestFinder {

    private final Class<?> _theClass;


    public ManifestFinder(Class<?> theClass) {

        _theClass = theClass;
    }


    public Manifest findManifest() throws MalformedURLException, IOException {

        String className = _theClass.getSimpleName();
        String classFileName = className + ".class";
        String pathToThisClass = _theClass.getResource(classFileName).toString();

        int mark = pathToThisClass.indexOf("!");
        String pathToManifest = pathToThisClass.toString().substring(0, mark + 1);
        pathToManifest += "/META-INF/MANIFEST.MF";
        return new Manifest(new URL(pathToManifest).openStream());

    }

}

parse the data from the manifest ( untested )

 String specificationVersion = manifest.getMainAttributes().getValue("Implementation-Version");

and then include the parsed version in the log statement

logger.info("Version " + specificationVersion + " of the application is currently running");

See the Jar file specification for more details.

so you can load your /META-INF/MANIFEST at runtime, using the ClassLoader. Then load and parse Implementation-Version:

String welcome = String.format("Version %s of the application is currently running",
                          implementationVersion);
logger.info(welcome);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top