Getting Automated Build Information For Your About Dialog3 min read

Most all applications have some sort of about dialog which users can access to get basic information about the application they are using. One thing that I feel is important is to know is the build number and date, essentially the version of the software being used.

Figure 1 About Dialog

For Java applications, this would require updating either a class variable or a file with current information. This is error prone and you may forget to change the values before releasing your software. I have written before about automating builds using Jenkins and this is another problem you can solve with Jenkins and an Ant build script.

Jenkins has a set of environment variables which are available for builds. The variable of importance for this issue is the BUILD_NUMBER variable. Each time Jenkins builds a project, this number is incremented. This gives you a way to know exactly which release the user has and you can, if needed, roll your software back to that instance in time using version control. To make this variable available, you need to add the following in your Ant script. You will then be able to access environment variables with “env.VARIABLE”.

<property environment="env" />

Another element necessary for solving this is how you update your application at build time. One solution is creating a file with the necessary data for your dialog. Then, when your application is running you can read this file information, parse it, and display the data in the dialog. This is where Ant comes in.

  <format property="TODAY" pattern="yyyy-MM-dd" />

In your Ant script, you need to format the TODAY property with your desired date format as shown above. The current application I am working uses yyyy-MM-dd. Next, you need to determine where you would like the file to be written. I want the file to exist in the same package as my dialog class. I read through my Ant script and determined the best place to insert the build information file. I located where all the source files are just prior to being copied to the build directory and inserted the file there. Then, when they are copied to the build directory  for compiling the informational file gets brought along.

<!-- Create build information file -->
    <echo file="project/src/ffl/dialog/buildInfo.txt" message="${env.BUILD_NUMBER}|${TODAY}" />

The above Ant task, echo, creates the file. A simple message with the BUILD_NUMBER property and the TODAY property is written to the file location specified with the file attribute. I use the pipe to delimit the data so the application can parse it easily. As you can see, the build number is ${env.BUILD_NUMBER} and the date is ${TODAY}. Now your application just has to read the informational data.

//Load the data from the build info file
InputStream inStream = this.getClass().getResourceAsStream("buildInfo.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
String dataString = reader.readLine();
String[] dataArray = dataString.split("\\|");

The example above shows how you can read the generated data file. Each time Jenkins builds your project the date and build number are automatically updated and whenever you view the about dialog you are able to know exactly which version is being used.

Leave a Reply

Your email address will not be published.