Jose Sandoval Google
 Resume     Book     Software     Drawings     Home     Subscribe to RSS feed Search Web Search josesandoval.com

Java: automatically increment build number
Monday, December 10, 2007

An application build number is one of the most useful debugging tools out there. When something in the field doesn't work, you need to know which version is broken.

There are different ways a version number could be automatically incremented; however, in Java, the easiest way is to use your Ant build file. I'm not going to re-invent the wheel here, so find a very good explanation on how to do it at:I will summarize the solution, though. First, modify your build.xml file as follows:

<target name="jar" depends="compile">
...
<property name="version.num" value="1.0"/>
<buildnumber file="build.num"/>


<manifest file="MANIFEST.MF">
...
<attribute name="Main-Class" value="YOUR_CLASS_NAME"/>
<attribute name="Implementation-Version" value="${version.num}.${build.number}"/>
...
</manifest>
</target>

I like to use version numbers of the format: "1.0.1." In your case, you can change the string to whatever convention you need to follow. Note the bold code above. Those should be your new entries in your typical Ant build file.

Second, once you have your "Implementation-Version" set up, you need to display the information from within your Java application. To do that you need code that looks like:

System.out.println(YOUR_CLASS_NAME.class.getPackage()
.getImplementationVersion());

YOUR_CLASS_NAME should be your calling class. And you can obviously make use of the value however you want to. In this snippet of code, I'm just dumping it to the console.

Note that this works if you are packaging your application into an executable jar file. For stand alone applications (single classes), you can still use Ant to read a counter file, and automatically compile a class that increments the value. At the moment, I don't have a need for this solution, but it should be straight forward implementation.


10:07 PM | 0 comment(s) |

Comments:


This page is powered by Blogger. Isn't yours?

Guestbook
© Jose Sandoval 2004-2009 jose@josesandoval.com