How To: Read web/META-INF/MANIFEST.MF in Web Application

Hi All, 

As you all know In web application inside web directory there is a META-INF directory. Inside which we have MANIFEST.MF that holds version information. I have to read the MANIFEST.MF from this directrory. Need the manifest info for the purpose of build info and have to show in footer of each page. It's easy to read it in case of JAR file and while searching on net I got mostly examples on jar,classpath related Manifest. But my requirement was to read web/META-INF/MANIFEST.MF file.

So I am posting here what I done:

Inside web application then using the ServletContext.getResourceAsStream
method should work and same I used.

// for a Servlet, you can get the ServletContext like this
 ServletContext aContext = getServletConfig().getServletContext();
 InputStream inputStream = aContext.getResourceAsStream("/META-INF/MANIFEST.MF");

In Spring application do following inside controller:

//Build Info
ServletContext aContext= getServletContext();
InputStream fis =aContext.getResourceAsStream("/META-INF/MANIFEST.MF");

Hope this helps you 🙂 . Do post your comments

About nitingautam

I am Tech Lead (Java/J2EE/ExtJs) with a MNC located @ Gurgaon.
This entry was posted in HowTo, Java. Bookmark the permalink.

4 Responses to How To: Read web/META-INF/MANIFEST.MF in Web Application

  1. Gliska says:

    Решил добавить RSS и получать новости, мне лично понравилось, что написал автор

  2. Zlatan says:

    Your post solved my problem 🙂

    Just to add for Spring 3.0:

    @Controller
    public class VersionController {
    @RequestMapping(“/version”)
    public @ResponseBody
    Map getAppVersionData(HttpServletRequest req) throws IOException {
    Map map = new HashMap();

    ServletContext aContext = req.getSession().getServletContext();
    InputStream fis = aContext.getResourceAsStream(“/META-INF/MANIFEST.MF”);

    Properties p = new Properties();
    p.load( fis );

    . . .

    Thanks!

  3. Sudhakar K says:

    Thanks,
    This really works.
    I need to get PROJECT path.
    Actually I’m developing a html mail. I save my template in one location. when uploaded to server the path?

    try {
    ServletContext aContext = request.getServletContext();
    String fis = aContext.getRealPath("/");
    System.out.println(fis);
    } catch (Exception e) {
    }

  4. Sudhakar K says:

    I got what i need from featuriz.
    hear is the code.

    try {
    ServletContext aContext = request.getServletContext();
    String fis = aContext.getResource("/").getPath().replaceAll("/WEB-INF/classes/", "");
    System.out.println(fis);
    } catch (Exception e) {
    }

Leave a Reply

Your email address will not be published. Required fields are marked *