Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put version to my java application - Netbeans

Is there any way that i can give a version number to my application in netbeans.And then access that version number inside my code.

Something similar to the Assembly number that we use in .Net.Is there anything like that in java or in netbeans...?

like image 759
Jinith Avatar asked Mar 05 '11 14:03

Jinith


2 Answers

Define an Implementation-Version in the manifest of the Jar at build time. It is common to use some form of the date as the version number. E.G. 14.07.28

The value can be retrieved in code using..

String version = this.getClass().getPackage().getImplementationVersion();

<tstamp>
    <format property="now" pattern="yy.MM.dd"/>
</tstamp>
...
<jar
    destfile="build/dist/lib/${jar.name}"
    update='true'
    index='true' >
    <manifest>
        <attribute name="Created-By" value="${vendor}"/>
        <attribute name="Implementation-Title" value="${application.title}"/>
        <attribute name="Implementation-Vendor" value="${vendor}"/>
        <attribute name="Implementation-Vendor-Id" value="org.pscode"/>
        <!-- This next property is retrieved in code above. -->
        <attribute name="Implementation-Version" value="${now}"/>
    </manifest>
    <fileset dir="build/share">
        <include name="${package.name}/*.class" />
        <include name="${package.name}/*.png" />
    </fileset>
</jar>

This comes from a build file for a project I have open at the moment. The relevant attribute is the last one in the manifest section.

like image 160
Andrew Thompson Avatar answered Oct 08 '22 19:10

Andrew Thompson


In my JavaFX app created in Netbeans, I can set the build version (Implementation version) in the Project Properties window shown here: Screenshot of Netbeans Project Properties

This number is then used everywhere, for example it is automatically inserted into the filename of the installer. It is also the number retrieved by this.getClass().getPackage().getImplementationVersion(); mentioned by the accepted answer.

like image 38
Ralph Avatar answered Oct 08 '22 21:10

Ralph