How would you write a build.xml
file, using neither custom code nor external dependencies (such as a shell script), that:
The easiest way of doing this is to change the problem. Instead of making the Any build do this for you, have whatever process that you're calling Ant calculate what the version number should be, and then pass that in as a property e.g.
ant -Dbuild.version=1.2.3
This has the flexibility of whatever build you're working with being able to take its cue from whatever, such as the SVN revision, the current date and time, or whatever.
ant -Dbuild.version=svnversion .
ant -Dbuild.version=date +"%Y%m%d%H%D"
ant -Dbuild.version=${major}.svnversion .
.date +"%Y%m%d%H%D"
etc. You can get pretty comprehensive if you want.
If you want to have an ever incrementing number, then you can store it in a file and then pass that in at compile time. For example, you can do:
VER=cat build.version
VER=$((VER+1))
echo $VER > build.version
Lastly, if you really want this to be in the build.xml file, the best thing to do is have a separate task to execute the increment-and-build option and fork off a nested ant build with your 'main' target. You'd thus end up with
ant -> ant -Dbuild.version=1.2.3.4 -> ...
In other words, given your build.xml with a (current) default of 'build', then change it to 'version' and have the ant 'version task do the calculation followed by a nested call to and build.
Implementation is left as an exercise to the reader, as is translating the approach to a non-UNIX platform.