Build and Version Numbering for Java Projects (ant, cvs, hudson)
For several of my projects I capture the subversion revision number, time, user who ran the build, and some system information, stuff them into a .properties file that gets included in the application jar, and read that jar at runtime.
The ant code looks like this:
<!-- software revision number --><property name="version" value="1.23"/><target name="buildinfo"> <tstamp> <format property="builtat" pattern="MM/dd/yyyy hh:mm aa" timezone="America/New_York"/> </tstamp> <exec executable="svnversion" outputproperty="svnversion"/> <exec executable="whoami" outputproperty="whoami"/> <exec executable="uname" outputproperty="buildsystem"><arg value="-a"/></exec> <propertyfile file="path/to/project.properties" comment="This file is automatically generated - DO NOT EDIT"> <entry key="buildtime" value="${builtat}"/> <entry key="build" value="${svnversion}"/> <entry key="builder" value="${whoami}"/> <entry key="version" value="${version}"/> <entry key="system" value="${buildsystem}"/> </propertyfile></target>
It's simple to extend this to include whatever information you might want to add.
Your build.xml
...<property name="version" value="1.0"/>...<target name="jar" depends="compile"> <buildnumber file="build.num"/> <manifest file="MANIFEST.MF"> ... <attribute name="Main-Class" value="MyClass"/> <attribute name="Implementation-Version" value="${version}.${build.number}"/> ... </manifest></target>...
Your java code
String ver = MyClass.class.getPackage().getImplementationVersion();
- Build numbers should be associated with a continuous integration server like hudson. Use different jobs for different branches/teams/distributions.
- To keep the version number in the final build, I would recommend just using maven for build system. It will create a .properties file archived into the final .jar/.war/.whatever-ar on
META-INF/maven/<project group>/<project id>/pom.properties
. The .properties file will contain the version property. - Since I am recommending maven, I would urge you to check out the release plugin to prepare the release on source repository and keep the versions on sync.