Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

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.

like image 359
Jay R. Avatar asked Apr 20 '09 22:04

Jay R.


2 Answers

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.

like image 73
Chris Dail Avatar answered Nov 10 '22 00:11

Chris Dail


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> 
like image 31
Dima Avatar answered Nov 09 '22 22:11

Dima