Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert version numbers in our java jars, that a user can access?

We have a library that gets released with a different version number every couple of weeks. The problem is that in order to store the version number in our jars we have a version.txt file that just contains the version number and then gets included into the build. This seems like the wrong way to do this but I can't come up with a better solution. What is a better way to store the version number in our jar's so that if a user calls me up I can easily find out the version of our product they are using?

like image 335
Grammin Avatar asked Jul 07 '11 13:07

Grammin


People also ask

How do I find the version number of a jar file?

In order to find out which Java version the jars are built or compiled, we can extract the jar file and use either javap or hexdump to check the . class file versions. There is also a MANIFEST. MF file in the jar file, which contains some header information about the JDK used.

What is Java jar version?

JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory. A JAR file can be created by the command-line jar tool, or by using the java. util. jar API in the Java platform.

Do jar files contain Java files?

A Java Archive, or JAR file, contains all of the various components that make up a self-contained, executable Java application, deployable Java applet or, most commonly, a Java library to which any Java Runtime Environment can link.


3 Answers

Firstly -- make sure your program or tool can some SHOW the version number. But where does it come from? We include it in the build.

Just make sure it's visible someplace when they run it! If there's nothing runnable, add a Main, and make it the Main-Class, that just prints the version. Then you can say, Please type java -jar YourLibrary.jar, and it just runs main and prints your version.

Here's the beginnings of the code to read resources out of your jar, from inside the jar, if the resource (such as Version.txt) is next to klazz:

ClassLoader loader = klazz.getClassLoader();
InputStream in = loader.getResourceAsStream (name);

I like to make it automatic in every build, so I don't forget to bump it. Rather than a text file, I use .properties... but you could do the same thing in Version.txt.

(Actually, at the moment, we include just the build-time. But the idea is the same.)

I do it like so -- I have a Version.properties file, with:

buildHost = @HOSTNAME@
buildTime = @BUILDTIME@
buildUser = @USERNAME@

And as part of the ANT script, we do:

<tstamp>
    <format property="BUILDTIME" pattern="yyyy.MM.dd.HH:mm:ss z" locale="en,UK" />
</tstamp>

<exec executable="hostname" outputproperty="HOSTNAME">
    <!-- note, this is unixey, of course -->
    <arg value="-s" />
</exec>

<property environment="env"/>
<property name="USERNAME" value="${env.USER}"/>
<property name="build.info" value="path/to/Version.properties" />
<copy file="${build.info}" tofile="${obj.dir}/${build.info}" overwrite="true">
    <filterchain>
        <replacetokens>
            <token key="BUILDTIME" value="${BUILDTIME}"/>
            <token key="HOSTNAME" value="${HOSTNAME}"/>
            <token key="USERNAME" value="${USER}"/>
        </replacetokens>
    </filterchain>
</copy>

Note -- the above is a bit platform specific, but you get the idea.

And how you read .properties files, it's another little pile of code but easy enough.

like image 195
david van brink Avatar answered Nov 14 '22 21:11

david van brink


Do you mean manual access for users, or also access from Java applications? You can create a MANIFEST.MF, which contains the version number. I think every major buildtool has some facilities to do that automatically during the build process.

like image 43
dunni Avatar answered Nov 14 '22 22:11

dunni


  1. Consider writing yourself a small class that reads your txt file. (Better yet, switch to a properties file as David suggests.)
  2. Give it a main method that prints that version on the console.
  3. Create a shell script or batch file that puts the .jar on the classpath and calls your class.
  4. Then, during your build process, have your build system insert the current build number/version/timestamp into that file. For ant, see Davids example, for Maven I recommend the Maven Build Number plugin.
  5. When you need to see the version of a .jar file, run your script.
like image 38
Urs Reupke Avatar answered Nov 14 '22 21:11

Urs Reupke