在Java应用程序中处理版本号的好方法是什么? [英] What is a good way to handle a version number in a Java application?

查看:803
本文介绍了在Java应用程序中处理版本号的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java和Swing Application Framework开发桌面应用程序。我有一个关于盒子的应用程序,我想让那个盒子包含一些关于正在测试的版本的指示。我倾向于以自动方式改变该值。我正在使用CruiseControl构建从SVN提交触发的应用程序。

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.

其他人使用什么机制来完成这项工作?是否有一个关于盒子版本号库或一组ant相关工具,我可以在构建过程中放置​​它?

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?

我不是在寻找部署选项或者无论如何要自动检查更新或类似的东西。我只是想问一下测试人员在关于框中的版本是什么,并得到一个可靠的答案。

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.

推荐答案

我会通过声明我使用 Apache Maven 构建来开始这篇文章。你也可以用Ant或其他工具做类似的事情,但这就是我用maven做的事情。

I will start this post off by stating that I use Apache Maven to build. You could also do a similar sort of thing with Ant or another tool, but this is what I have done using maven.

我发现处理这个的最好方法是使用项目的版本加上subversion版本作为内部版本号。从maven你可以包括以下内容。这将为您提供subversion版本号$ {scm.revision}。

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

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

然后,我将其用作jar文件清单的一部分作为实现版本。

After you have then, I then use this as part of the jar file manifest as the Implementation Version.

  <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();

这为您提供了完整的内部版本号,例如1.0.13525,其中最后一个数字是颠覆修订版。有关设置此内容的更多信息,您可以查看完整的博客文章我在这个问题上做了一段时间。

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 post I did a while back on this very issue.

这篇关于在Java应用程序中处理版本号的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆