在 Java 应用程序中处理版本号的好方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/770364/
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
What is a good way to handle a version number in a Java application?
提问by Jay R.
I am developing a desktop app using Java and Swing Application Framework. I have an application about box and I'd like to have that box contain some indication of what version is being tested. My preference is for that value to be changed in an automated fashion. I'm using CruiseControl to build the application triggered off the commits to SVN.
我正在使用 Java 和 Swing 应用程序框架开发桌面应用程序。我有一个关于 box 的应用程序,我想让那个 box 包含一些正在测试的版本的指示。我更喜欢以自动方式更改该值。我正在使用 CruiseControl 来构建由对 SVN 的提交触发的应用程序。
What mechanism to others use to do this job? Is there an about box version number library or set of ant related tools that I can just drop in place in my build process?
其他人使用什么机制来完成这项工作?是否有一个 about box 版本号库或一组与 ant 相关的工具,我可以在我的构建过程中放置到位?
I'm not looking for deployment options or anyway to automatically check for updates or anything like that. I just want to be able to ask a tester what version is in the about box and get a reliable answer.
我不是在寻找部署选项,也不是在寻找自动检查更新或类似的东西。我只是希望能够询问测试人员关于框中的版本并获得可靠的答案。
采纳答案by Chris Dail
I will start this post off by stating that I use Apache Mavento build. You could also do a similar sort of thing with Ant or another tool, but this is what I have done using maven.
我将通过声明我使用Apache Maven进行构建来开始这篇文章。你也可以用 Ant 或其他工具做类似的事情,但这是我使用 maven 所做的。
The best way I have found to handle this is to use the version of your project plus the subversion revision as the build number. From maven you can include the following. This will give you the subversion revision number as ${scm.revision}.
我发现处理这个问题的最好方法是使用你的项目版本加上 subversion 版本作为内部版本号。在 maven 中,您可以包含以下内容。这将为您提供 ${scm.revision} 形式的颠覆版本号。
<build>
<plugins>
<plugin>
<artifactId>maven-scm-plugin</artifactId>
<executions>
<execution>
<id>getting-scm.revision</id>
<phase>validate</phase>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
After you have then, I then use this as part of the jar file manifest as the Implementation Version.
之后,我将其用作 jar 文件清单的一部分作为实现版本。
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifestEntries>
<Implementation-Version>${this.version}.${scm.revision}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
The nice thing about this is that you can access this from code by using the following:
这样做的好处是您可以使用以下代码从代码中访问它:
Package p = getClass().getPackage();
String version = p.getImplementationVersion();
That gives you the full build number like "1.0.13525" where the last number is the subversion revision. For more information on setting this up you can check out the full blog postI did a while back on this very issue.
这为您提供了完整的内部版本号,例如“1.0.13525”,其中最后一个数字是颠覆版本。有关设置的更多信息,您可以查看我不久前就这个问题所做的完整博客文章。
回答by Charlie Martin
You're making it too complicated. SVN provides the version number as a macro, like CVS.
你把它弄得太复杂了。SVN 以宏的形式提供版本号,就像 CVS 一样。
Create a simple inverface containing a constant, and have SVN instantiate that through a macro, like
创建一个包含常量的简单接口,并让 SVN 通过宏实例化它,例如
public interface IVersionNumber {
public static final String version = "$Id$" ;
}
You need to tell SVN to use keywords on that file, as
您需要告诉 SVN 在该文件上使用关键字,如
$ svn propset svn:keywords "Id" IVersion.java
$ svn propset svn:keywords "Id" IVersion.java
You can read about properties here.
回答by Dima
Have the build script create a property file holding the version. It's good idea to grab the revision # directly from SVN. This way you can refer to it in tests. Place this property file into the packaged jar and read it in run time. We usually have major and minor versions set as parameters to ant scrpt while revision is managed automatically by SVN and gives consistent build number for the references.
让构建脚本创建一个包含版本的属性文件。直接从 SVN 获取修订版本号是个好主意。这样你就可以在测试中引用它。将此属性文件放入打包好的jar中,运行时读取。我们通常将主要和次要版本设置为 ant scrpt 的参数,而修订版由 SVN 自动管理,并为引用提供一致的内部版本号。
This snippet target runs svn command line and outputs into a temp file (svndump). Then constructing xx.yy.zz string, place it in another file which will be later included into jar. Note that xx.yy are taken from external parameters, this is major-minor version.
此代码段目标运行 svn 命令行并输出到临时文件 (svndump)。然后构造 xx.yy.zz 字符串,将其放在另一个文件中,该文件稍后将被包含到 jar 中。注意 xx.yy 取自外部参数,这是主要-次要版本。
<target name="getrev">
<exec executable="svn" output="svndump">
<arg value="info"/>
<arg value="${my.svn.url}"/>
</exec>
<property file="svndump"/>
<property name="my.build" value="${Revision}"/>
<property name="my.rev" value="${my.ver}.${my.build}"/>
<echo message="current revision is ${my.rev}"/>
<property name="my.dir.dist" value="${my.dir.root}/dist/${my.rev}"/>
<echo message="${my.rev}" file="${my.dir.projects}/revision"/>
<delete file="svndump"/>
</target>
回答by Esko Luontola
With Maven I've used the buildnumber-maven-pluginwhich gets the revision number from Subversion or uses a sequence or a timestamp, which is then is used to replace a ${buildNumber}
placeholder where ever you like. I suppose that other build tools and continuous integration servers have similar features. Search your favorite search engine with "<your-tool> build number".
在 Maven 中,我使用了buildnumber-maven-plugin,它从 Subversion 获取修订号或使用序列或时间戳,然后用于替换${buildNumber}
您喜欢的占位符。我想其他构建工具和持续集成服务器也有类似的功能。使用“<your-tool> 内部版本号”搜索您最喜欢的搜索引擎。
回答by John Munsch
We've used JReleaseInfofor a long time. You can set it up in your Ant script (we don't use Maven for our projects) and it bumps the version number automatically. It also has some nice additions in that it generates a class containing all the information. You can just call that class within your code to get strings ready to use or call the class from the command line to have it print out the version number without needing to change your code any.
我们已经使用JReleaseInfo很长时间了。您可以在您的 Ant 脚本中设置它(我们的项目不使用 Maven),它会自动增加版本号。它还有一些不错的附加功能,因为它生成了一个包含所有信息的类。您可以在代码中调用该类以准备好使用的字符串,或者从命令行调用该类以使其打印出版本号,而无需更改您的代码。
回答by Jay R.
Since I'm using cruise controland Antto build everything, I ended up at least for now, using the ${label}property passed into the ant call for building the project. In my ant file, I use the Ant "replace" taskto change the properties file that holds the about box resource named Application.version to include that label.
由于我使用巡航控制和Ant来构建所有内容,因此至少现在我使用传递到 ant 调用中的${label}属性来构建项目。在我的 ant 文件中,我使用 Ant “替换”任务来更改包含名为 Application.version 的 about 框资源的属性文件,以包含该标签。
I'm planning to change from the label incrementerto the svn label incrementerand then use the svn revision number that way. Its just a little more work and it requires that I make changes to the cruise control config file.
我打算从标签增量器更改为svn 标签增量器,然后以这种方式使用 svn 修订号。它只是更多的工作,它需要我对巡航控制配置文件进行更改。