JavaBlog.fr / Java.lu DEVELOPMENT,Java,Spring,WEB Java/Spring: Get and display the version from MANIFEST.MF

Java/Spring: Get and display the version from MANIFEST.MF

In this small post, I will expose your simple useful manners to get the version from MANIFEST.MF and display it in an web application.

1st case: loading of the META-INF file contained in a custom library

  1. Create (or check the presence of) the MANIFEST.MF file in the META-INF folder of your custom library like MyLib\src\META-INF\MANIFEST.MF.
    The content of this file could be:

    Manifest-Version: 1.0
    Class-Path: 
    Implementation-Title: MyApplication
    Implementation-Version: 1.2.3
    Implementation-Vendor: JavaBlog.fr
    
  2. Create a class named Version with a static attribute in your library MyLib\src\main\java\com\ho\mylib\util\Version.java:
    public class Version {
        public final static String CURRENT_VERSION =  Version.class.getPackage().getImplementationVersion();
    }
    
  3. And in your web application, the JSP displaying the library version could be like:
    <%@ page import="com.ho.mylib.util.Version" %>
    <h2 class="x-panel-header"><%=Version.CURRENT_VERSION%></h2>
    

2nd case: loading of the META-INF file contained in a classic web application

  1. Create (or check the presence of) the MANIFEST.MF file in the META-INF folder of your web application like MyApplication\WebContent\META-INF\MANIFEST.MF or MyApplication\war\META-INF\MANIFEST.MF.
    The content of this file could be:

    Manifest-Version: 1.0
    Class-Path: 
    Implementation-Title: MyApplication
    Implementation-Version: 1.2.3
    Implementation-Vendor: JavaBlog.fr
    
  2. And in your web application, the JSP displaying the library version could be like:
    <%
    //Get version of application
    java.util.Properties prop = new java.util.Properties();
    prop.load(getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
    String applVersion = prop.getProperty("Implementation-Version");  
    %>
    <h2 class="x-panel-header"><%=applVersion%></h2>
    

3rd case: loading of the META-INF file contained in a Spring web application

  1. Create (or check the presence of) the MANIFEST.MF file in the META-INF folder of your web application like MyApplication\WebContent\META-INF\MANIFEST.MF or MyApplication\war\META-INF\MANIFEST.MF.
    The content of this file could be:

    Manifest-Version: 1.0
    Class-Path: 
    Implementation-Title: MyApplication
    Implementation-Version: 1.2.3
    Implementation-Vendor: JavaBlog.fr
    
  2. Create a singleton class ApplicationVersion which will contain the version of application from “/META-INF/MANIFEST.MF” file:
    package com.ho.spring3.security.test1.util;
    
    import java.util.Iterator;
    import java.util.Properties;
    
    import javax.servlet.ServletContext;
    
    import org.springframework.web.context.ServletContextAware;
    
    /**
     * Bean containing the version of application from "/META-INF/MANIFEST.MF" file.
     * @author huseyin
     *
     */
    public class ApplicationVersion implements ServletContextAware{
    	
    	// ----------------------------------- PRIVATE ATTRIBUTES
    	private static String CURRENT_VERSION = null;
    	private static ApplicationVersion instance = null;
    	private static ServletContext servletContext = null;
    
    	// ------------------------------------- PUBLIC FUNCTIONS
    	private ApplicationVersion(){}
    
    	public static synchronized String getVersion(){
    		if(instance == null){
    			try {
    				instance = new ApplicationVersion();
    				String name = "/META-INF/MANIFEST.MF";
    				Properties props = new Properties();
    				props.load(servletContext.getResourceAsStream(name));
    				
    				/*for (Iterator iterator = props.keySet().iterator(); iterator.hasNext();) {
    					Object key = iterator.next();
    					Object value = props.get(key);
    					System.out.println(key+"="+value);
    				}*/
    				
    				instance.CURRENT_VERSION = (String) props.get("Implementation-Version");
    			} catch (Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		
    		return instance.CURRENT_VERSION;
    	}
    	
    	public void setServletContext(ServletContext servletContext){
    		this.servletContext = servletContext;
    	}	
    }
    
  3. Modify the Spring context to add the newly created bean:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="
    	http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://www.springframework.org/schema/tx
    	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.0.xsd
    	">
    	
    	<bean name="applicationVersion" class="com.ho.spring3.security.test1.util.ApplicationVersion"/>
    
    </beans>
    
  4. And in your web application, the JSP displaying the library version could be like:
    <%@ page import="com.ho.spring3.security.test1.util.ApplicationVersion"%>
    
    <%=ApplicationVersion.getVersion() %>
    

Note: The MANIFEST.MF could be generated or updated manually or via MAVEN, ANT.

That’s all!!!

Best regards,

Huseyin OZVEREN

1 thought on “Java/Spring: Get and display the version from MANIFEST.MF”

  1. I went with your 3rd case: loading of the META-INF file contained in a Spring web application and it worked like a charm. Thanks for the examples.

    -Jeff

Leave a Reply to Jeff Cancel reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post

Backup your projectsBackup your projects

Hello again, Here, I expose a simple solution to backup your developments in the remote directory \\YOUR_SERVER_or_EXTERNALDISK\backupWorkspaces\. To do this: – Create file “backup.dat” in your Eclipse workspace ie “R:\java\dev\eclipse_workspaces\default-3.3”: –