Wednesday, July 19, 2006

Build Numbers in jars build with Maven

Build Numbers for Maven builds using Cruise Control.

Maven is a very useful tool. For the most part, you can get it to do whatever you want, but the trick is finding out how. One issue that my team had was:
  • We're using Cruise Control
  • We'd like to have the CC build number in the Jar Manifests
  • We're using Maven to build.
There are a couple of ways that you could do this, using ant from within maven, but that seems like extra work or at least extra complexity. The solution we found was:

Configure the project POM file to add a manifest to the jar. Most of the following is just using the maven Plugin. There is a great guide to manifests on the maven site so I'll only show the parts relevant to configuring the manifest to add a cruise control build number.
      <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.berczuk.ManifestDemo</mainClass>
<packageName>com.berczuk</packageName>
<addClasspath>true</addClasspath>
<addExtensions />
</manifest>
<manifestEntries>
<buildNumber>${build.number}</buildNumber>
</manifestEntries>
</archive>
</configuration>
</plugin>
This will set a buildNumber field in the manifest to the value of the build.number property. The trick is to get the property into maven. To do this you take advantage of the properties that cruise control makes available to you. I am using the cruise control AntBuilder to launch maven. So my cruise control config.xml looks like:
<ant antscript="/usr/local/ant/bin/ant"
buildfile="checkout/demo/build.xml"
uselogger="true" usedebug="true" target="cruisecontrol.build">
<property name="maven.path" value="/usr/local/maven2/bin/mvn"/>
</ant>
Implicitly, CruiseControl makes some properties available to you:
<target name="cruisecontrol.build" depends="cvsUpdate">
<mkdir dir="target" />
<exec executable="${maven.path}" dir="${basedir}" failonerror="true" output="build.out" logError="true">
<arg line="-Dbuild.number=${label} clean package assembly:assembly install" />
</exec>
</target>
by adding the line -Dbuild.number=${label} you can pass the CC label for the build on to maven.

Introduction

I'm not going to claim that this blog will have a theme. But one thing I've been meaning to collect was tips and tricks for using tools that help software teams to work more effectively. One such tool is Maven. In the right context, maven2 is a wonderful tool. The trick is trying to find out how to do things. And once I find out, I tend to forget until a friend asks me a related question. So one thing I hope to do is to collect interesting things that I've discovered. Along the way I'll probably also have some opinions to express. If you found your way here, I hope that you find something useful.