How to determine the Buildversion of a webapplication out of a JAR Manifest file

Another Post because I need this code snippet sometimes and don’t want to search the whole internet every time. The following code snippet opens the Manifest file an reads the Implementation properties:

	public String getBuildVersion() {

		FacesContext facesContext = FacesContext.getCurrentInstance();

		if (facesContext != null) {
			if (facesContext.getExternalContext().getContext() instanceof ServletContext) {
				ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();

				if (servletContext != null) {

					Properties prop = new Properties();
					try {
						prop.load(servletContext.getResourceAsStream("/META-INF/MANIFEST.MF"));
					} catch (IOException e) {
						String msg = "Could not read the MANIFEST.MF for getting the VersionInfo";
						LOG.error(msg, e);
						return msg;
					}

					String implementationTitle = prop.getProperty("Implementation-Title");
					String implementationVendor = prop.getProperty("Implementation-Vendor");
					String implementationVersion = prop.getProperty("Implementation-Version");

					return implementationTitle + " Build-Id:" + implementationVersion + " by:" + implementationVendor;

				}
			}
		}
		return "Unable to determine Softwareversion";

	}

 

Leave a Reply

Your email address will not be published.

*