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:

    1Manifest-Version: 1.0
    2Class-Path:
    3Implementation-Title: MyApplication
    4Implementation-Version: 1.2.3
    5Implementation-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:
    1public class Version {
    2    public final static String CURRENT_VERSION =  Version.class.getPackage().getImplementationVersion();
    3}
  3. And in your web application, the JSP displaying the library version could be like:
    1<%@ page import="com.ho.mylib.util.Version" %>
    2<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:

    1Manifest-Version: 1.0
    2Class-Path:
    3Implementation-Title: MyApplication
    4Implementation-Version: 1.2.3
    5Implementation-Vendor: JavaBlog.fr
  2. And in your web application, the JSP displaying the library version could be like:
    1<%
    2//Get version of application
    3java.util.Properties prop = new java.util.Properties();
    4prop.load(getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
    5String applVersion = prop.getProperty("Implementation-Version"); 
    6%>
    7<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:

    1Manifest-Version: 1.0
    2Class-Path:
    3Implementation-Title: MyApplication
    4Implementation-Version: 1.2.3
    5Implementation-Vendor: JavaBlog.fr
  2. Create a singleton class ApplicationVersion which will contain the version of application from “/META-INF/MANIFEST.MF” file:
    01package com.ho.spring3.security.test1.util;
    02 
    03import java.util.Iterator;
    04import java.util.Properties;
    05 
    06import javax.servlet.ServletContext;
    07 
    08import org.springframework.web.context.ServletContextAware;
    09 
    10/**
    11 * Bean containing the version of application from "/META-INF/MANIFEST.MF" file.
    12 * @author huseyin
    13 *
    14 */
    15public class ApplicationVersion implements ServletContextAware{
    16     
    17    // ----------------------------------- PRIVATE ATTRIBUTES
    18    private static String CURRENT_VERSION = null;
    19    private static ApplicationVersion instance = null;
    20    private static ServletContext servletContext = null;
    21 
    22    // ------------------------------------- PUBLIC FUNCTIONS
    23    private ApplicationVersion(){}
    24 
    25    public static synchronized String getVersion(){
    26        if(instance == null){
    27            try {
    28                instance = new ApplicationVersion();
    29                String name = "/META-INF/MANIFEST.MF";
    30                Properties props = new Properties();
    31                props.load(servletContext.getResourceAsStream(name));
    32                 
    33                /*for (Iterator iterator = props.keySet().iterator(); iterator.hasNext();) {
    34                    Object key = iterator.next();
    35                    Object value = props.get(key);
    36                    System.out.println(key+"="+value);
    37                }*/
    38                 
    39                instance.CURRENT_VERSION = (String) props.get("Implementation-Version");
    40            } catch (Throwable e) {
    41                e.printStackTrace();
    42            }
    43        }
    44         
    45        return instance.CURRENT_VERSION;
    46    }
    47     
    48    public void setServletContext(ServletContext servletContext){
    49        this.servletContext = servletContext;
    50    }  
    51}
  3. Modify the Spring context to add the newly created bean:
    01<?xml version="1.0" encoding="UTF-8"?>
    06    xsi:schemaLocation="
    13    ">
    14     
    15    <bean name="applicationVersion" class="com.ho.spring3.security.test1.util.ApplicationVersion"/>
    16 
    17</beans>
  4. And in your web application, the JSP displaying the library version could be like:
    1<%@ page import="com.ho.spring3.security.test1.util.ApplicationVersion"%>
    2 
    3<%=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

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post