Appending software version to a JAR filename - java

I would like to append the output JAR filename of a Netbeans project with some version number: something like 1.0, 2.0b or even a Subversion revision number. I can't seem to find anything on this, though. I am also not sure if this would the responsibility of the build system (Ant) or if the IDE (Netbeans) can delegate the process. Is there a centralised, clean way of doing this?

IMO, this is the responsibility of the build system, not of the IDE. Let me say it in other way: don't rely on your IDE to build your project, use a build tool. Using an IDE is fine during development but being IDE dependent to build a project is not a good thing (what if you change your IDE tomorrow, what if you want to build your project on another machine/OS without that IDE, what if you want to build your project on a headless machine, what if you want to automate your build, what if someone wants to build that project and doesn't have that IDE, etc, etc). Really, this is what build systems are for.
Now, regarding your initial request, there are plenty ways to add a version number. One of them is to use the Ant's BuildNumber task:
This is a basic task that can be used to track build numbers.
It will first attempt to read a build number from a file (by default, build.number in the current directory), then set the property build.number to the value that was read in (or to 0, if no such value). It will then increment the number by one and write it back out to the file. (See the PropertyFile task if you need finer control over things such as the property name or the number format.)
Use it for example like this:
<target name="jar" depends="compile">
<property name="version.num" value="1.00"/>
<buildnumber file="build.num"/>
<jar destfile="foo-${version.num}-b${build.number}.jar"
basedir="."
includes="**/*.class"
/>
</target>
Or you could indeed add subversion revision number. An easy way to do this seems to install the SVNAnt task and use the status task:
<target name="revisionnumber">
<!-- get the svn revision number -->
<svn>
<status path="application.cfm" revisionProperty="svn.revision" />
</svn>
<echo>Sandbox Revision: ${svn.revision}</echo>
</target>
Finally, another option would be to use Maven instead of Ant which has a built-in version management feature as pointed out by cetnar.

I'm not sure if it's the best way, but we put it in MANIFEST.MF file like this:
Implementation-Version: 2.0b
We can get this value programmatically like this:
String version_num = this.getClass().getPackage().getImplementationVersion();

If you feel like using a tool to handle your builds then there are lots about, such as CruiseControl, which is ANT based and has pretty deep integration with your source code control.
I use it to automatically increment a build number and use that as the last digit in my version number for the jar, e.g. 1.4.168, where 168 is the build number. I am just about to get it to put a label into CVS just before the fetch with the build number so I know exactly what code is in the jar.

Well is done default by Maven. Even if you want name your jar file with more detailed information you can use build number plugin.
EDIT
At begining I misunderstood your question so following part relates to adding this information inside jar files.
You can do it yourself by creating manifest file. In Maven it you can tune proces of creating manifest file by additional configuration. I suppouse (I'm sure) that in Ant should be similar functionality.

you can use maven for vesion and read it from pom.
read this article:
Embedding the maven version number
at
http://happygiraffe.net/blog/2008/10/01/embedding-the-maven-version-number/

Related

Is it possible to to modify variables during the build with Ant?

Hi so recently I have just discovered the amazing power of Ant and I was wondering if Ant has the following capability.
Let me first explain what I am trying to do... I have a product that requires different build specifications for each build and I would hate to do it manually (the site is not live yet but I am expecting a fair amount of orders/day). I can't just build a jar for each combination of settings mostly because each jar will need to have a customer's user id and their license built in. Something like the following is the desired effect:
private final String license = "0123-4567-8910";
private final int userId = 1337;
To clarify: the above values would be set by passing arguments through the command line (hopefully).
You can use Ant's <replace> tag for this.
suppose your log4j properties contains placeholder as
log4j.properties
log4j.appender.R.File=LOGS_DIR_PATH/JBulletinBoard.log
Then you can do like
<replace file="log4j.properties" token="LOGS_DIR_PATH" value="D:/logs"/>
I am not exactly sure what you are trying to do and I will avoid the debate about Maven vs Ant as life is to short..
Its very possible to externalize a properties file and have a common ant file.
Tell ant about your property file
<property file="licence.properties" />
and you can refer to your properties in that file like
<echo message="Registered licence = ${build.licence}" />

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?
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.
Consider writing yourself a small class that reads your txt file. (Better yet, switch to a properties file as David suggests.)
Give it a main method that prints that version on the console.
Create a shell script or batch file that puts the .jar on the classpath and calls your class.
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.
When you need to see the version of a .jar file, run your script.
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.

Where can I find tutorials about ant properties?

I'm trying to learn some ant for a Struts 1.x project that I was thrown onto. Mainly I'm trying to find a good referent for the inherent variables/properties of ant...beginners tutorial. Any GOOD reference really.
A couple lines of the ant file that I've been trying to figure out just for example...
<available file=${deploy.ant.docbase.dir}/WEB-INF/sun-web.xml" property="sun.web.present"/>
and
<replace file="${temp.sun.web}">
<replacetoken><![CDATA[<!DOCTYPE]]></replacetoken>
<replacevalue<![CDATA[<!-- <!DOCTYPE]]></replacevalue> //in ant is <!-- the comment out flag?
</replace>
I did do some searching and only could find ant build examples without explanation, but if it is covered and I just didn't find it a link will suffice. No reason to make someone reexplain it....I just couldn't find it.
Your first code block refers to the "available" ant task. It sets the property sun.web.present if the given file exists.
In your second code block, "<!--" starts an XML comment ("-->" closes one). This is true for all XML, not just ant build.xml files. In this case it is using the "replace" ant task to replace "<!DOCTYPE" with "<!-- <!DOCTYPE" within the file named by temp.sun.web.
In general an ant build file has targets like "build" or "clean". These depend on each other so that "test" runs "build" first. The targets are implemented by "tasks", where each XML tag refers to a task. You can read their manual and refer to the per-task docs for how each task works.
The Ant Manual is your friend. There's a link Ant tasks on the left side of the page. Click on that link, and then the List of Tasks link. That will list all of the Ant tasks on the left and their explanation on the right. There, you'll see the available task and the replace task.
Unfortunately, the Ant manual uses Frames (bad Ant Manual! Bad Ant Manual!), so I can't supply a link that will list both the
(Shameless bid for reputation)
The example doesn't use a built-in property. Most Ant targets won't, because after properties are first set they are immutable. Instead, Ant scripts usually define their own properties. The Ant manual lists the properties that Ant predefines.
If you want to get into the guts of Ant, I recommend the Manning "Ant in Action" book.

Group and counting a String in Ant

I have the following problem. I have something like 300 Eclipse Plugins. Now, as part of an ant script I want to read all MANIFEST.MF files and then look for the execution environment string.
Bundle-RequiredExecutionEnvironment: J2SE-1.4
Now, this string has several possible values. I want to create a report that lists the execution environment for each plug-in. That part is not really a problem as I can use some kind of regexp to obtain it.
My problem is that I want also to create some kind of summary for tracking changes at a glance, something like:
JS2E-1.4: 50 Plugins
JS2E-1.5: 150 Plugins
JS2E-1.6: 74 Plugins
Anyone has some suggestions on how could I go around this?
EDIT: Reason for using ANT is that I want to integrate it with a nightly build script
I would definitively go for hard-coded Ant task and decompose the problem in two tasks:
the first task takes a jar file and outputs a plugin-info.xml file that contains various infos, like the environment
the second task parses all these xml files and creates an XML summary report
This will of course generate (n+1) XML files for n plugins and some will find this way too much.
The nice end effect with that approach is that you can generate either detail or aggregated reports very easily (with some XSLT magic.) or even graphs.
If i were to do it myself, i probably would just write a perl script.
If it has to be done from Ant, i would write an Ant Task to do it.
I would suggest just printing each executable environment on System.out and then post process with "|sort| uniq -c".
You can use the math task from the ant-contrib project
I had to do it, I'd probably go for some shell script or custom code

How to get system user in ant (within eclipse)?

How can I access the name of the system user (which eclipse also uses for the javadoc
author tag) in my ant build file?
I'm trying to show some information about the current program version in my java application.
I decided to use jreleaseinfo which passes variables from my ant build script to my java classes (to show them in a window). With svnant I'm even capable of accessing the latest revision number and build date from svn within my build.xml.
Now: The last thing I need is to show who made that build!
This will work anywhere. It uses the java system property user.name.
<property environment="env" />
<echo message="user: ${user.name}" />
user.name can be used:
<echo>User is: ${user.name}</echo>
Results in:
[echo] User is: coobird

Categories