How would you write a build.xml
file, using neither custom code nor external dependencies (such as a shell script), that:
- Generates a build number of the form major.minor.revision (e.g., 01.02.34).
- Auto-increments the revision on each compile of the source code.
- Auto-increments the minor version on each execution of a dist(ribution) task.
Additionally:
- Provides an option to increment the major number.
- Provides an option to increment the minor number.
- Whenever the major number is incremented, the minor and revision numbers are set to 0.
- Whenever the minor number is incremented, the revision number is set to 0.
Bonus:
- Creates a variable based on the
git
revision number (like a subversion revision number).
Clarification:
- Automatic checkout (or commit) is not required.
- Integration with Subversion is not desired.
Thank you for any examples. Here are some related sites that describe how to perform similar tasks:
- Create a Build Number with Ant.
- Using the BuildNumber Ant task.
- Ant and Build Version Numbers.
In my project i don’t increase the minor and major number automatically. We set it from our global build properties. Like that:
That’s because they will be changed for releases (not for test or other builds) and committed together with other sources (we have be able to build some old or branch version).
But if you want to increase the minor number, you can do it like the build number in my example.
We can use conditions to check if we should increase the micro,minor and major version.
Increase minor if micro is 9, and so on.
This solution does increment minor or revision number automatically if a compile or a dist target has been selected. The incrementation can be switched off if one of the following properties has been set:
If the property inc.major has been set, then the major number will be incremented and the other both values will be set to zero. The SHA-1 checksum is being calculated by the textual representation of the version file.
By the way: If would have been allowed, you could create your own ant task in java script, which is included in JDK 6.
Now here's the ant file
Build Process
build_info.properties
will be created during build in your project folder You could write all information about your build in this file.During Development
After first build the file build_info.properties will be placed in the repository. You can change and commit any number (major, minor, build numbers) by your self when ever you want, or increase it automatically during build like build.number in the example below.
svnant Example
Using svnant 1.3.0:
This was a while ago, so this is from memory:
I build a custom CruiseControl.Net labeller block that ticked up the build number on each build. It maintained an XML file with all 4 components of the version number and identified each project by name (so it could support multiple projects).
The four values it generated get passed to the build process (nAnt, in our case), which had the responsibility of tweaking all the AssemblyInfo.cs files to reflect the proper build number.
Edited to note: The only value that get automatically ticked up was the build number. Major/Minor version numbers were specified in the CC.Net project configuration. The build number restarted at 0001 for each change in major or minor revision number (e.g., if you went from version 7.1 to version 7.3, for instance, the 7.1 build might be at build number 783, but the first 7.3 build started with build number 1; the next 7.1 build would be build 784.
Change version numbers merely required tweaking 1 setting the CC.Net config file.
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.versionLastly, 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.