将版本放入我的 Java 应用程序 - Netbeans

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5204297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:56:24  来源:igfitidea点击:

Put version to my java application - Netbeans

javanetbeansversion

提问by Jinith

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.

有什么方法可以为我在 netbeans 中的应用程序提供版本号。然后在我的代码中访问该版本号。

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

类似于我们在 .Net 中使用的程序集编号。在 java 或 netbeans 中是否有类似的东西......?

回答by Andrew Thompson

Define an Implementation-Versionin 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

Implementation-Version在构建时在 Jar 的清单中定义一个。通常使用某种形式的日期作为版本号。例如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.

这来自我目前打开的项目的构建文件。相关属性是清单部分中的最后一个。

回答by Ralph

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

在我在 Netbeans 中创建的 JavaFX 应用程序中,我可以在此处显示的“项目属性”窗口中设置构建版本(实现版本): Netbeans 项目属性的屏幕截图

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.

然后该号码会在任何地方使用,例如它会自动插入到安装程序的文件名中。它也是this.getClass().getPackage().getImplementationVersion();被接受的答案提到的检索到的数字。

回答by duffymo

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.

我不知道 .NET 程序集编号,但如果您正在创建 Web 应用程序,您当然可以将版本号放入 WAR 文件的清单中。

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

任何 Java 包都可以添加一个构建信息文本文件,以便您可以了解这些内容。

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

您的版本号可以是来自 Ant 的内部版本号、来自 Subversion 的版本号或两者的组合。