Best practices for embedding SVN version number in Java jar manifest?
Asked Answered
R

1

6

I see questions like How to get revision number from subversion using maven? but are there any standards (defacto or otherwise) for which actual fields to use in the manifest, for the version # and for the repository URL (i.e., showing that it's on the trunk vs. a branch)? I've seen Implementation-Version used, with and without a leading "r" (for revision), I've seen Implementation-Build: used as well.

edit I removed the maven tag, that shouldn't have been there. My question is about the jar contents, not the tooling per se.

Relucent answered 18/1, 2012 at 1:54 Comment(4)
No standards that I'm aware with. Every company/developer seems to have an opinion on how to manage revision numbers, never mind how to record and describe that revision :-)Monobasic
You might want to consider using a file other than (or at least in addition to) the manifest if you ever want to read the revision number programatically. Reading MANIFEST.MF from inside the JVM is actually quite a pain and can be inconsistent from one environment to another if the classloader hierarchy changes at all.Duff
see also: #7671102Relucent
ha, glad this is a Popular Question. Now I just need to get around to doing this m'self.Relucent
C
3

Unfortunately, jar manifests by itself do not have any standard for version numbering.

But, actually, there is another standard way of getting revision number updated automatically. You could use svn:keywords in order to get current revision number in your files after each commit. There is $Revision$ property for revision substitution and $HeadURL$ for repository URL substitution. You just need to put following string into the file and place this file under version control:

$Revision$ $HeadURL$

If you create manifest on the fly with maven, I would recommend putting following content into version.properties file:

revision=$Revision$
repourl=$HeadURL$

Then include in into pom.xml with the statement (maven should have properties plugin enabled):

<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-1</version>
    <executions>
      <execution>
        <phase>initialize</phase>
        <goals>
          <goal>read-project-properties</goal>
        </goals>
        <configuration>
          <files>
            <file>version.properties</file>
          </files>
        </configuration>
      </execution>
    </executions>
  </plugin>

And then you will be able to put revision number and repo url to the manifest:

<manifest>
  <attribute name="Revision" value="${revision}" />
  <attribute name="Repository URL" value="${repourl}" />
</manifest>

Please also note that you will need to explicitly enable svn:keywords using subversion properties in order to get $Revision$ and $HeadURL$ substituted in your file with actual values. If you will decide to use version.properties, you will need to run following command:

svn propset svn:keywords Revision version.properties
svn propset svn:keywords HeadURL version.properties
Colophon answered 19/1, 2012 at 15:14 Comment(1)
thanks, one correction is that should be "Revision\ HeadURL" instead of "Id" in the propset. However, that $Revision only includes the version of version.properties ( see: svnbook.red-bean.com/en/1.7/… "where's globalrev" ) However, the $HeadURL$ doesn't change often (unless you svn switch a subdirectory). So I should consider using HeadURL and not ignore keywords completely. Thanks.Relucent

© 2022 - 2024 — McMap. All rights reserved.