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!

18 thoughts on “Revision and build numbers for your NetBeans apps

  1. Hi,

    there seems to be something missing after

    Add the following lines below the by replacing [Package Name] and [App Name] with your own values.

    and

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

    Doesn’t seems to be working anymore, bellow everything else? Below ?

    I’m using netbeans 6.9

    Do you have a solution? Would be much appreciated

  2. Hi Sebastian!

    Thanks for pointing this out. It seems like the syntax highlighter WP-Syntax didn’t handle the WordPress editor too well and ended up escaping all relevant code. I’ve replaced the highlighter with a new one, so the code should be up and running again. Seems like it has been missing for quite some time, so again, thanks for finding this error.

    If the method described in this post still works for Netbeans 6.9, please give me a note. I haven’t tested it since 6.7.

  3. Thanks for this useful post. Works for me in NB7. I made a few changes to output the version to a HTML template so it could be included in the footer.

    ${Application.version}

  4. First of all, thanks for useful post! For all of us who need to use national characters (like czech čžšř…) might be useful to change propertyfile tasks entries to something like this:
    <propertyfile file="${src.dir}/encodingTest/resources/EncodingTestApp.properties" jdkproperties="true"
    at least with Ant 1.8.2 and unmodified entry it broke escaped characters in .properties file every second run of ant, modified entry delete some comments, but key-value pairs are as they should be

  5. Hey! Thanks for the tip. However, it is not working for me in Netbeans 6.9. Whatever change I make to my properties file is never reflected in my app. It’s as if Netbeans was undoing any changes I make. I did as you said:

    Application.title = ArbBook v{Application.version}
    Application.version = 0.9.{Application.buildnum}

    However my title bar keeps showing ‘ArbBook’ only (the value prior to my mod) and if I go to the About box the version num. is fixed at ‘1.0’. Any clue of what am I doing wrong?
    Thanks again for your time.

    1. Check if value of Application.buildnum key in property file is increasing, this means, that build.xml is workink properly, if it’s not increasing, you’ve probably bad path to property file. Then check in NB, if version label is associated with application scope property Application.version, I think its default is class scope property file, which is [YourProject]AboutBox.properties by default. And finally, in your code snippet dollar signs before compound brackets are missing, so check this as well.

      1. Hi again! Thanks for the prompt response. You’re right, I totally missed the $ signs! :P Still, no changes at all. The new Application.buildnum property is not being written, and I’m pretty sure my paths are ok. As previously, any changes I make by hand to my App.properties seem to do nothing. I checked my AboutBox.properties and it actually refers to variables like ${Application.title} and ${Application.version} so it should be affected by these. Also, if I change this file manually nothing happens! It’s as if these files were being recreated from scratch everytime I build the app or something… This is weeeird!

        1. Found it!
          “Please pay attention that NB in “compile on save” mode doesn’t monitor changes in properties files, so you need to clean/build entire project. ”
          The changes were simply not being copied to the build dir…

  6. Hi, We are running into an interesting with the .properties file. We are using Maven for building and packaging our source code. As a part of the build we are generating a .properties file which is updated with the version number of one of our 3rd party dependencies. The issue we are running into is , when we do a clean build the version number gets updated correctly, however when we try to run the project the version number is overwritten back to it’s variable name .
    For example if the property file has a variable called {twitter.api.jar.version} . this variable gets replaced by the correct value (in this case it’s the version of the Twitter jar dependency) only when we do a clean build. if we subsequently run the project then for some reason the variable value is replaced by the variable name .

    Any idea what could be going wrong here? Appreciate if you have any inputs

    1. Hi! I’m afraid I can’t remember experiencing anything similar, so off the top of my head I don’t have any solution. I would suggest having a look at the build.xml file to see if there is anything happening for a run target or similar. Perhaps you have added something like

      <entry key="twitter.api.jar.version" value="${twitter.api.jar.version}">

      without the $-sign?

  7. It is as Simba has described (probably meant {twitter.api.jar.version}).

    I have a .properties file, maven project, netbeans 7.2. When I do a clean build, the src properties file appears in the target directory with the variable correctly expanded, per maven filtering.

    However, when I then do run or debug from the IDE, the variable is immediately set back to its symbolic name in the target properties file, rendering the session useless.

    Simba, you are not hallucinating. This is a problem (imho).

    -David

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>