Revision and build numbers for your NetBeans apps

After going through a couple of guides on the net, I figured out how to add build numbers and revision numbers to my NetBeans projects. The build number will be incremented for each build, and the revision number is gathered from your subversion repository. If you don't use Subversion just skip that part and add the revision number manually.

In this tutorial I assume you are creating an application based on the NetBeans Desktop Application wizard. Otherwise, you may just follow along and use whatever fits into your project. All you need is a java project being built by Ant (as all NetBeans projects are).

Adding a build number

First of all, figure out where the properties file for your application is located. This should be in [package name].resources (or if you are browsing the files, you will find it under /src/[package name]/resources) and is named the same as your application with the suffix ".properties".

This could for instance be myapp.resources/myapp.properties or /src/myapp/resources/myapp.properties.

Open it up and find the version property. It should look something like this:

Application.version = 1.0

Change this to the following

Application.version = 1.0.0.${Application.buildnumber}

Now, we are ready to add a build number to your build.xml file. This is found in the nbproject folder if you browse for files (not in the Projects browser).

Add the following lines below the

<import file="nbproject/build-impl.xml"/>

by replacing [Package Name] and [App Name] with your own values.

<target name="-post-jar" description="Sets the buildversion for the current build">
    <propertyfile file="${src.dir}/[Package Name]/[App Name].properties">
        <entry key="Application.buildnumber" value="1" type="int" operation="+"/>
    </propertyfile>
</target>

This should now add your build number to your version number. If you want to have the version in the main title bar of your application just go back into the properties file and change the Application.title property to something like this:

Application.title = My Application Name ${Application.version}

Adding the Subversion revision number

Go back to your build.xml file and add the following below everything else:

<target name="-post-init" description="Sets the buildversion for the current build">
    <exec outputproperty="svna.version" executable="svnversion">
        <arg value="-c" />
        <redirector>
            <outputfilterchain>
                <tokenfilter>
                    <replaceregex pattern="^[0-9]*:?" replace="" flags="g"/>
                    <replaceregex pattern="M" replace="" flags="g"/>
                </tokenfilter>
            </outputfilterchain>
        </redirector>
    </exec>
    <propertyfile file="${src.dir}/[Package Name]/[App Name].properties">
        <entry key="Application.revision" value="${svna.version}" type="int" operation="="/>
    </propertyfile>
    <echo>Revision found from SVN: ${svna.version}</echo>
</target>

Then, add the revision number to your version number by editing changing the Application.version property to something like this:

Application.version = 1.0.${Application.revision}.${Application.buildnumber}

This should be all it takes. Just leave a comment if it does not work out as expected.

Working with national characters

If you'd like to use national characters, you have to edit the propertyfile line like this:

<propertyfile file="${src.dir}/[Package Name]/[App Name].properties" jdkproperties="true">

Thanks to Poli for this tip!