How to insert version numbers in our java jars, th

2020-02-26 09:09发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

  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.


回答4:

I'm with Reupke on this one.

Store the number however you like, in a properties file, or a txt file, plain text, encrypted, in an embedded DB.. doesn't matter how you store it as long as you have it.

Then, I would write class with a main method that reads the value in an prints it out nicely to the screen... That way if the user wants to know their version number then can simply run the command and get this version.