Put version to my java application - Netbeans

浪子不回头ぞ 提交于 2019-12-03 17:26:31

问题


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...?


回答1:


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.




回答2:


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

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.




回答3:


I don't know about .NET assembly numbers, but if you're creating a web application you can certainly put a version number into the manifest of your WAR file.

Any Java package can have a build info text file added to it so you can tell these things.

Your version number could be a build number from Ant, a version number from Subversion, or a combination of the two.



来源:https://stackoverflow.com/questions/5204297/put-version-to-my-java-application-netbeans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!