What is a good way to handle a version number in a Java application?

前端 未结 6 807
[愿得一人]
[愿得一人] 2020-12-23 10:25

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 ver

相关标签:
6条回答
  • 2020-12-23 10:47

    With Maven I've used the buildnumber-maven-plugin which 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".

    0 讨论(0)
  • 2020-12-23 10:50

    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.

    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.

    <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>
    
    0 讨论(0)
  • 2020-12-23 10:54

    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.

    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>
    

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

    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.

    0 讨论(0)
  • 2020-12-23 10:55

    Since I'm using cruise control and Ant to 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" task to change the properties file that holds the about box resource named Application.version to include that label.

    I'm planning to change from the label incrementer to the svn label incrementer and 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.

    0 讨论(0)
  • 2020-12-23 11:00

    We've used JReleaseInfo for 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.

    0 讨论(0)
  • 2020-12-23 11:02

    You're making it too complicated. SVN provides the version number as a macro, like CVS.

    Create a simple inverface containing a constant, and have SVN instantiate that through a macro, like

    public interface IVersionNumber {
        public static final String version = "$Id$" ;
    }
    

    You need to tell SVN to use keywords on that file, as

    $ svn propset svn:keywords "Id" IVersion.java

    You can read about properties here.

    0 讨论(0)
提交回复
热议问题