Local versioning on NetBeans

Supposing you want to mark the build number on your JAR files. Java packages are frequently versioned by changing the filename, but this is an unconfortable method for the administrators trying to deploy the applications. The recommended way is to rename the storage (be it the rest of the URL or the folder on the local disk) and to specify the implementation version on the MANIFEST.MF file.

NetBeans uses the Ant automating tool, taking it to a higher level, automating even the building of the Ant script.

MANIFEST.MF is dinamically built, in part by the JAR tool provided by Sun and in part by the Ant script. In the root folder of the Netbeans project there is a YourProjectFile/build.xml file (the usual name for default Ant scripts), but it is pretty small to be the one in charge. Opening it, you may see that it is just a wrapper for another XML Ant script, YourProjectFile/nbproject/project.xml. The latter is not editable, but you may override some tasks formally declared in project.xml with that purpose.

I decided to insert two fields in the YourProjectFile/nbproject/project.properties, that is two lines:

minor.number=1

major.number=1

The file is editable and Netbeans won’t overwrite your custom fields.

Then I edited build.xml, overriding the -pre-jar or -post-compile tasks like this:

<target name="-pre-jar">

  <propertyfile file="./nbproject/project.properties">

    <entry key="minor.number" type="int" operation="+" value="1" pattern="0000"/>

    <entry key="major.number" type="int" default="1"/>

  </propertyfile>

  <manifest file="MANIFEST.MF" mode="update">

    <attribute name="Implementation-Version" value="${major.number}.${minor.number}"/>

  </manifest>

</target>

This script will increment the minor build value stored in YourProjectFile/nbproject/project.properties and also in the MANIFEST.MF file. I decided that major versions may be changed by hand.

You may try to go further and fix the template that generates the project.xml file. This is /org/netbeans/modules/java/j2seproject/resources/build-impl.xsl in the file named YourNetbeansFolder/ide5/modules/org-netbeans-modules-java-j2seproject.jar.

Leave a Reply