Use ANT to Build a JAR with version/build numberTag(s): Environment
set ANT_HOME=c:\ant set JAVA_HOME=C:\Progra~1\Java\jdk1.5.0 set PATH=%ANT_HOME%\bin;%JAVA_HOME%\bin;%path%
package howto; public class Hello { public static void main( String[] args ){ System.out.println( "Hello World" ); } }
> javac howto/Hello.java > jar -cvf hello.jar howto/Hello.class added manifest adding: howto/hello.class(in = 415) (out= 284) (deflated 31%) > java -cp hello.jar howto.Hello Hello World
<project default="buildHello"> <target name="compile"> <javac srcdir="." /> </target> <target name="buildHello" depends="compile" /> </project>
> ant compile Buildfile: build.xml compile: [javac] Compiling 1 source file
<project default="buildHello"> <target name="compile"> <javac srcdir="." /> </target> <target name="jar" depends="compile"> <jar destfile="hello.jar" basedir="." includes="**/*.class" /> </target> <target name="buildHello" depends="compile,jar" /> </project>
> ant jar Buildfile: build.xml compile: jar: [jar] Building jar: /Dev/hello.jar BUILD SUCCESSFUL Total time: 2 seconds $ jar -tvf hello.jar jar -tvf hello.jar 0 Wed May 03 17:06:32 EST 2006 META-INF/ 55 Wed May 03 17:06:32 EST 2006 META-INF/MANIFEST.MF 55 Wed May 03 17:06:32 EST 2006 howto/ 335 Wed May 03 16:36:16 EST 2006 howto/Hello.class
> java -cp Hello.jar howto.Hello Hello World
<project default="buildHello"> <target name="compile"> <javac srcdir="." /> </target> <target name="jar" depends="compile"> <delete file="hello.jar"/> <delete file="MANIFEST.MF"/> <manifest file="MANIFEST.MF"> <attribute name="Built-By" value="${user.name}"/> <attribute name="Main-Class" value="howto.Hello"/> </manifest> <jar destfile="hello.jar" basedir="." includes="**/*.class" manifest="MANIFEST.MF" /> </target> <target name="buildHello" depends="compile,jar" /> </project>
> ant jar ... > java -jar hello.jar Hello World
<project default="buildHello"> <target name="compile"> <javac srcdir="." /> </target> <target name="jar" depends="compile"> <delete file="hello.jar"/> <delete file="MANIFEST.MF"/> <property name="version.num" value="1.00"/> <buildnumber file="build.num"/> <manifest file="MANIFEST.MF"> <attribute name="Built-By" value="${user.name}"/> <attribute name="Main-Class" value="howto.Hello"/> <attribute name="Implementation-Version" value="${version.num}-b${build.number}"/> </manifest> <jar destfile="hello.jar" basedir="." includes="**/*.class" manifest="MANIFEST.MF" /> </target> <target name="buildHello" depends="compile,jar" /> </project>
Implementation-Version: 1.00-b1
Implementation-Version: 1.00-b2
package howto; public class Hello { public static void main( String[] args ){ System.out.println( "Hello World "); System.out.println("version : " + Hello.class.getPackage().getImplementationVersion() ); } }
<project default="buildHello"> <target name="compile"> <javac srcdir="." /> </target> <target name="jar"> <delete file="hello.jar"/> <property name="version.num" value="1.00"/> <buildnumber file="build.num"/> <manifest file="MANIFEST.MF"> <attribute name="Built-By" value="${user.name}"/> <attribute name="Main-Class" value="howto.Hello"/> <attribute name="Implementation-Version" value="${version.num}-b${build.number}"/> </manifest> <jar destfile="hello.jar" basedir="." includes="**/*.class" manifest="MANIFEST.MF" /> </target> <target name="cleanup"> <delete> <fileset dir="." includes="**/*.class"/> <fileset file="MANIFEST.MF"/> </delete> </target> <target name="buildHello" depends="compile,jar,cleanup" /> </project>
> ant buildHello ... > java -jar hello.jar Hello World version : 1.00-b3
Build number is great but a Built date is useful too!
... <target name="jar"> <delete file="hello.jar"/> <property name="version.num" value="1.00"/> <buildnumber file="build.num"/> <tstamp> <format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" /> </tstamp> <manifest file="MANIFEST.MF"> <attribute name="Built-By" value="${user.name}"/> <attribute name="Main-Class" value="howto.Hello"/> <attribute name="Implementation-Version" value="${version.num}-b${build.number}"/> <attribute name="Built-Date" value="${TODAY}"/> </manifest> <jar destfile="hello.jar" basedir="." includes="**/*.class" manifest="MANIFEST.MF" /> </target>
See also this Howto.