Automatic Version Numbering in Web Applications with Hudson (Part 1)

Creating a web application deployed to a multitude of servers (or even to 1 server only) will put you to the challenge to find out what are you actually looking at when opening its URL. At good old windows time you could “compile” a major/minor version string into the executable and at least check the exe-files properties. There is no equivalent if you open a website. You could choose to put a version manually at your page (the least advanced and error prone way) or you make your automatic build (CI) server (that also might deploy to test systems) to write a time and version stamp into a property file (which happens to be the MANIFEST.MF file which usually gets little to no attention) and make the web application self-aware of its version number and print it somewhere on the index page.
I came across a couple of online discussions with solutions using the InputStreamReader, but it didnt worked for me. Either it reads null or the manifest file of some other jar files in the classpath.

This tutorial cover 3 parts:

  1. Create a web application that reads from the manifest file.
  2. Extend the ant script and make Hudson adding the build number and ID into the war file.
  3. Create a ZK application that reads from the manifest file.

Information on the manifest file

  • The MANIFEST.MF is a file inside the META-INF directory which is optional. The Netbeans IDE creates it. Actuall ant is creating it. Guess it became de-facto standard without many people really looking at this file(s).
    By default it contains usually only one line “Manifest-Version: 1.0”
    References: link1, link2
  • Infos on the ANT manifest task (link)

Requirements for this tutorial

  • Netbeans 6.8
  • Glassfish V3

    (earlier might work as well, but I did not test it)

Tutorial Part 1 (Create a web application that reads from the manifest file)

  • Create a new Web Application VersionServletDemo

    New Web Application

    New Web Application

    New Web Application

  • Default MANIFEST.MF
    The default file is under src/conf. It will be copied to the build path once you build the application.
    Remark: If you change the file (in src) you need to build and re-deploy. Deploy on save would not populate the changes into the build.

    Default MANIFEST.MF

  • Create a scriptlet that reads MANIFEST.MF
    We will read the default attribute Manifest-Version, since we didnt change the file yet.
    Change the default index.jsp to this

    <%@page import="java.util.Properties" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd">
    
    <%
     Properties prop = new Properties();
     prop.load(getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
     String applVersion = prop.getProperty("Manifest-Version");
    %>
    
    <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>JSP Page</title>
     </head>
     <body>
     <h1>Hello World!</h1>
    
     Version: <% out.println(applVersion); %>
    
     </body>
    </html>
    

    Run:

    Web Application with Version

  • Alternatively read from a servlet

    package servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Properties;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet(name = "VersionServlet", urlPatterns = {"/VersionServlet"})
    public class VersionServlet extends HttpServlet {
    
     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
     response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();
     try {
     Properties prop = new Properties();
     prop.load(getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF"));
     out.println("All properties:" + prop.stringPropertyNames());
     out.println("<br>");
     out.println("Version: " + prop.getProperty("Manifest-Version"));
     } finally {
     out.close();
     }
     }
    
     @Override
     protected void doGet(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
     processRequest(request, response);
     }
    }
    
  • Change the source MANIFEST.MF
    Use different attributes, rebuild and try to read them by yourself.
Advertisements

One thought on “Automatic Version Numbering in Web Applications with Hudson (Part 1)

  1. Pingback: Automatic Version Numbering in Web Applications with Hudson (Part 2) « The JavaDude Weblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s