Objective:
To demonstrate a way to retrieve java package version by programatically.
Demonstration Steps:
Step-1: Generate Version Info using Maven
Click to visit maven pom changes here
To add package version info into the bundle as war. To achieve this, need to add some additional setting parameter in pom.xml of web project like below.
1 2 3 4 5 |
<archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> |
Above parameter will add a META-INF/MENIFEST.MF file which holds package information in below format.
1 2 3 4 5 |
Manifest-Version: 1.0 Implementation-Title: blogweb Maven Webapp Implementation-Version: 0.0.1-SNAPSHOT Implementation-Vendor-Id: blogroot Built-By: devendrasingh |
Implementation-Version holds the current package version which is 0.0.1.SNAPSHOT
Step-2 Read MENIFEST.MF using Java
Click to visit Java Implementation to read the version from file
1 2 3 4 5 6 7 8 9 10 |
try { final ServletContext application = getServletConfig().getServletContext(); try (InputStream inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF")) { Manifest manifest = new Manifest(inputStream); response.getWriter() .print("Application Version " + manifest.getMainAttributes().getValue("Implementation-Version")); } } catch (Exception e) { LOGGER.error("Error in servlet {}", e); } |
Conclusion:
Finally, the output of application version in browser will be
1 |
Application Version: 0.0.1-SNAPSHOT |
Alert!
Same thing you can be implemented by rest services as well but the core logic will be remain as it is.
Note: Eclipse does not create MENIFEST.MF file in war exploded version in target folder but add this file to the WAR. IntelliJ and other maven based IDE used WAR exploded version therefore there won’t be any issue with in IDE except eclipse.