0

We'd like to have the version of the java source code, in our case the svn $Id$ string, embedded in the generated class file.

We'd like to be able to determine this information from a static inspection of the class file, preferably by running the strings or what command.

A naive attempt to declare a private final static String variable set to this value inside each class didn't result in a legible string embedded in the class file.

Chaim Geretz
  • 826
  • 5
  • 23
  • How does your version string look like? three bytes `1.0` are very easy to overlook in a binary file. – John Dvorak Jan 17 '13 at 20:31
  • See also [What is a serialVersionUID and why should I use it?](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it). That might prove interesting. – Richard JP Le Guen Jan 17 '13 at 20:36
  • @Jan thanks. I was testing with an unexpanded "$Id$" string, once I checked it into svn and got it back in expanded form I noticed it in the output of the strings command – Chaim Geretz Jan 17 '13 at 22:01
  • Do you need the version in the class file? or do you want your java app to return the version number? typically this would be put in the jar/war/ear manifest file. – thekbb Jan 17 '13 at 22:29
  • We aren't using _ar packaging mechanism so it would need to be in each class file – Chaim Geretz Jan 29 '13 at 20:38

2 Answers2

2

You said ... preferably by running the strings or what command ...

Let's assume you have something like this in your code:

private static final String SVN_ID = 
    "$Id: SvnIdDemo.java 1081 2008-09-30 19:03:23Z john $";

The following Perl one-liner ...

$ perl -nwe 'print "$1\n" if /.*(\$Id:[^\$]+\$).*/' SvnIdDemo.class

... prints the SVN ID string to STDOUT.

$Id: SvnIdDemo.java 1081 2008-09-30 19:03:23Z john $
oheyder
  • 121
  • 3
  • 1
    The given Perl syntax works on a Unix like system. On Windows you cannot use single-quotes for the mini script. You need double-quotes instead which requires you to escape the inner double-quotes with a backslash. – oheyder Jan 17 '13 at 21:03
1

You could add a method to the bottom of each class with a predefined name. Say you used:

public String extractChaimGeretzVersionNumber() {
    return "$Id$";
}

Then you find the version with a program that loads the class and, via reflection, calls the magic method and retrieves the version.

You would either have to have code that inserted the method to the .java files before building the .class and .jar files OR you could have a build step that checked that the magic method was already in every one of them. Fail the build if not found.

Lee Meador
  • 12,829
  • 2
  • 36
  • 42
  • 1
    When I did this sort of thing one time, I would just put something in the jar file that tied to the version control. So you tag VC, extract all the source, build the tag into the manifest (or have the build script write a class that returns the tag from a static method), and build the jar. If you needed to know what version of a class file is present, get the tag from the jar (or have it displayed on the screen or logged) and go retrieve the code versions from the version control. – Lee Meador Jan 17 '13 at 20:41