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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With