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

Lets continue with the application from part 1(link) wit a static MANIFEST.MF file to dynamically created attributes using the ANT manifest task.

Requirements for this tutorial

  • Netbeans 6.8
  • Glassfish V3
  • Hudson (local running good enough, how-to’s on Hudson setup you find in my earlier Tutorials)

Tutorial Part 2

  • Open the web application project from part 1

  • Open the build.xml (not the build-impl.xml which is maintained by Netbeans)
    Add the manifest task and extend the post-compile target.

    ...
     <import file="nbproject/build-impl.xml"/>
    
    <target name="-post-compile">
     <property environment="env" />
     <tstamp>
     <format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
     </tstamp>
     <manifest file="${build.web.dir}/META-INF/MANIFEST.MF">
     <attribute name="builduser" value="${user.name}"/>
     <attribute name="version" value="1.2.345"/>
     <attribute name="date" value="${TODAY}"/>
     </manifest>
    </target>
    ...
    
  • Update the Scriptlet
    We can read now the real version and date string that is dynamically created while building in Netbeans.

    <%@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 appVersion = prop.getProperty("version");
     String appDate = prop.getProperty("date");
     String versionString = "Version: " + appVersion + " (" + appDate +")";
    %>
    
    <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>JSP Page</title>
     </head>
     <body>
     <h1>Hello World!</h1>
    
     <h5><% out.println(versionString); %></h5>
    
     </body>
    </html>
    


  • Run the application

    Web Application with Version

  • Update the build.xml to use Hudson variables
    Hudson comes with a list of environment variables that are available for shell scripts or can be used in ant scripts. You find the complete list here (if you run hudson as a local standalone server), for now we focus on BUILD_NUMBER and BUILD_ID.

    <target name="-post-compile">
     <property environment="env" />
     <manifest file="${build.web.dir}/META-INF/MANIFEST.MF">
     <attribute name="builduser" value="${user.name}"/>
     <attribute name="version" value="${env.BUILD_NUMBER}"/>
     <attribute name="id" value="${env.BUILD_ID}"/>
     <attribute name="tag" value="${env.BUILD_TAG}"/>
    <attribute name="server" value="${env.BUILD_URL}"/>
     </manifest>
    </target>
    
  • Execute build from Hudson
    (I skip the part of setting up Hudson, creating a job, etc. In earlier tutorials I described that in detail)
    The resulting MANIFEST.MF (we look at the file in the job workspace)

    MANIFEST.MF with Hudson variables

    MANIFEST.MF with Hudson variables

Remaining Challenges:

  • Automatic deployment to test servers after the successful build
  • Use the Hudson variables only if they are available otherwise put a different/default content there. If you build from Netbeans (with the Hudson variables being unknown)

    Netbeans Build

Advertisements

2 thoughts on “Automatic Version Numbering in Web Applications with Hudson (Part 2)

  1. Pingback: Automatic Version Numbering in Web Applications with Hudson (Part 3) « 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