Thursday, February 12, 2009

Automated release tagging with ANT

Sometime back I was researching how to automatically tag a CVS snapshop from an ANT script. Yes, I know this should be a basic thing :-) But for some reason, IT has always taken care of tagging and creating builds. Whenever I needed to release some code, I would tag the CVS snapshot from Eclipse, I would manually creat a MANIFEST.MF file, and then create the distributable.

However, this time I tried to automate the task with an ANT script.

What I needed was an ANT script which would automatically create a MANIFEST.MF file with a release number, and tag the CVS project with that release number.

The first thing I came across was the Ant cvs task. This task allows us to run any CVS command on a repository. However, there is a catch. You need CVS installed on your local machine for this to work. Under the hoods the CVS task invokes cvs.exe (on Windows), or cvs on *nix machines. This was a clear show stopper. I did not want to make all developers install CVSNT on their development machines.

So, I started looking for CVS libraries implemented in Java. There are a couple of them. I decided to settle with javacvs which is created by the Netbeans folks. javacvs is a CVS client implementation written purely in Java. If you are interested, here is a javacvs quick start tutorial. It outlines some non trivial steps for setting up and using javacvs.

I downloaded javacvs and wrote some code to tag my repository. However, when I ran it, the library threw a NullPointerException. The javacvs folks have not created Javadocs for their projects, so the only option I had to figure out what was wrong, was to look at their source code. I followed a link from their page to download the sources. However, the page suggested that the sources were moved to a Mercurial repository. They were also merged with the Netbeans source, so I would now have to download all of Netbeans to get the javacvs sources.

Ok, so I installed Mercurial and ran this to get the Netbeans sources.

hg clone

It seemed to be doing something, but it was also taking forever. After downloading some 350 MB of stuff, it still seemed to be getting more. The command line did not give me any status message, so I had no clue how much was downloaded and how long it would take. To make matters worse, we had some network problems on that day and the Internet was really dragging. After a couple of hours, I gave up and aborted the checkout. After coming home I tried to resume the download by running the above command, but it failed. Looks like partially cloned Mercurial repositories cannot be resumed. I would have to delete all the downloaded files and restart the clone operation.

Meanwhile, I came across a class called CvsCommand in javacvs, which was like a command line for javacvs. It could take any cvs command with arguments and run it on the CVS repository. I ran the following from my project directory. This class was also set to be the main-class of the jar file. So executing the jar as shown below results in the main method of CvsCommand being invoked.

java -jar org-netbeans-lib-cvsclient.jar tag b20090211_01

It almost worked. It read CVSROOT details from the ./CVS/Root file, but it complained that it could not find the password for my CVS account. Some research suggested that I needed a .cvspass file in my home directory. But how was I to create a .cvspass file? It had to be created automatically. I copied .cvspass from my Linux machine into the home directory of my Windows machine. But javacvs continued complaining about the password. So, I manually modified the ./CVS/Root file in the project directory and added a password to the cvsroot string. The format is


Finally everything worked. Running the program automatically tagged the CVS repository with a releaseNumber.

I could run the above command using ANT's java task. However, I still had to manually specify the build number in the Ant script. Ant also has a buildNumber task, which can create incremental numbers. This task relies on a file to store a number. Every time it is run it takes the number from the file and saves it in memory in an Ant property called build.number. At the same time it also rewrites the file with an incremented number. By default it uses a file called build.number in the same directory as the Ant script. However, we can specify another file if we want. Ok, so this gave me incremental numbers, but I still wanted to use a timestamp in the build number. Ant has a task called tstamp for just that. When run, it's default behavior is to store the current time (yyyymmdd) in an Ant property called TODAY. I could concatenate the output from tstamp and buildNumber to create a release tag.

I still wanted to automatically create a MANIFEST.MF file with the release number. Ant also has a manifest task, which creates a MANIFEST.MF file from supplied parameters.

Here's a simple build file that uses Ant's buildNumber, tstamp, manifest, and java tasks to automatically tag CVS with a release number and create a manifest file. To make a release, run this file first followed by your regular ANT script which creates the distributable file.

<project name="MyProject" basedir="." default="tag">
  <target name="build_number">
    <tstamp />
    <property name="" value="release_${DSTAMP}_${build.number}" />

  <target name="generate_manifest" depends="build_number">
    <manifest file="WebContent/META-INF/MANIFEST.MF">
      <attribute name="Built-By" value="${}"/>
      <attribute name="Product-Name" value="MyProject" />
      <attribute name="Date" value="${TODAY}" />
      <attribute name="Tag" value="${}" />
      <attribute name="Class-Path" value="" />

  <target name="tag" depends="generate_manifest">
    <echo message="tagging with build number ${}" />
    <java jar="lib/org-netbeans-lib-cvsclient.jar" fork="true">
      <arg value="tag" />
      <arg value="${}" />

No comments: