How do I create a versioned jar file using Ant?
Author: Deron Eriksson
Description: This tutorial describes how to create a versioned jar file using Ant's buildnumber task
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1

Page:    1 2 3 >

This tutorial will use the AntSW 'buildnumber' task to automatically increment a minor version number for a project when we build a jarW file, and it will also feature a 'major-version-number' property that we can manually update when we want to indicate that a major change has occurred. The version number will be included in the name of the jar file, and it will also be placed in the MANIFEST.MF file. The demonstration project is shown below.

team-cakes-utilities project in Navigator View

The team-cakes-utilities build.xml file is shown below. As you can see, it contains the project's 'major-version-number' property, which is set to 1. It references the and build-main.xml files in the ant-utilities project.


<?xml version="1.0" encoding="UTF-8"?>
<project name="team-cakes-utilities" default="" basedir=".">

	<property name="major-version-number" value="1" />
	<property name="project-name" value="${}" />
	<property file="../ant-utilities/" />
	<import file="../ant-utilities/build-main.xml" />

In build-main.xml in ant-utilities, the 'jar-version' target builds our versioned jar file. It calls the 'buildnumber' task, which creates a build.number file at the root level of team-cakes-utilities project (this file name can be specified using the 'file' attribute of 'buildnumber'. If the build.number file already exists, the task increments the build number within the build.number file by 1. The target creates a 'version-number' property which is the concatenation of the major-version-number, a period, and the build.number property that the 'buildnumber' task created. This version number is appended to the jar file name, and it's also included in the MANIFEST.MF file.

The 'build-jar-version' target compiles our jar classes to the bin directory using the 'compile-jar-classes' target, creates the 'build-version' directory if it doesn't already exist, and calls the 'jar-version' target to build the jar file and place it in the 'build-version' directory.

	<target name="jar-version">
		<buildnumber />
		<property name="version-number" value="${major-version-number}.${build.number}" />
		<jar basedir="bin" destfile="build-version/${project-name}-${version-number}.jar">
				<attribute name="Built-By" value="${builder}" />
				<attribute name="Built-On" value="${build-info.current-date}" />
				<attribute name="Built-At" value="${build-info.current-time}" />
				<attribute name="Implementation-Version" value="${version-number}" />

	<target name="build-jar-version">
		<antcall target="compile-jar-classes" />
		<mkdir dir="build-version" />
		<antcall target="jar-version" />

(Continued on page 2)

Page:    1 2 3 >