How do I package and use a customized task?
Author: Deron Eriksson
Description: This Ant tutorial shows how to package a custom Ant task into a jar file and reference that in a build.xml file
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 3 >

In another tutorial, I demonstrated custom-writing an AntSW task. I created an Ant 'var' task based on the com.cakes.ant.Variable class that could used as a variable in Ant, which is fairly useful since an Ant property can't be changed after it has been initially set. In this tutorial I'll package up that task and use it in the multiple-project build-main.xml file that I've been building up over the course of several tutorials.

The structure of the 'teamcakes-ant-util' project from the last tutorial is shown below. It features a build.xml file and Variable class.

'teamcakes-ant-util' project

In the last Ant tutorial, we created the project's build.xml file to demonstrate the difference between our variable task and the Ant property element. In this tutorial, we'd like to package up our task. Since it's a JavaSW class, we can do this by packaging the class in a jarW file. In our 'ant-utilities' project that we created, we already have the capability of building jar files and versioned jar files. Since we've already done this work, let's reference the ant-utilies build-main.xml and build-main.properties files. We can accomplish that with the following:

	<property file="../ant-utilities/build-main.properties" />
	<import file="../ant-utilities/build-main.xml" />

I also created the 'create-teamcakes-ant-util-versioned-jar' target that creates a 'build-version' directory for our jar file and then builds the jar file. The modified build.xml file is shown here:

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="teamcakes-ant-util" default="" basedir=".">

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

	<taskdef name="var" classname="com.cakes.ant.Variable" classpath="bin" />

	<target name="property-test">
		<property name="myProp" value="value1" />
		<echo>myProp is ${myProp}</echo>
		<property name="myProp" value="value2" />
		<echo>myProp is ${myProp}</echo>
	</target>

	<target name="variable-test">
		<var name="myVar" value="value1" />
		<echo>myVar is ${myVar}</echo>
		<var name="myVar" value="value2" />
		<echo>myVar is ${myVar}</echo>
	</target>

	<target name="variable-test-2" depends="variable-test">
		<echo>myVar is ${myVar}</echo>
	</target>

	<target name="create-teamcakes-ant-util-versioned-jar">
		<mkdir dir="build-version" />
		<antcall target="jar-version" />
	</target>

</project>

The ant-utilities build-main.properties file is included below:

ant-utilities build-main.properties

 

The ant-utilities build-main.xml file is included below:

ant-utilities build-main.xml


(Continued on page 2)

Page:    1 2 3 >