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 >

(Continued from page 1)

To keep things simple, since EclipseSW compiles our *.java files to *.class files in our project's /bin directory, let's just use AntSW to build a versioned jarW file out of Variable class using the .class file that Eclipse builds. In a 'real' work environment, it's probably best to use Ant to compile your *.java files to *.class files, but let's keep things simple and not do that for this example.

Our 'ant-utilities' project contains the 'create-teamcakes-ant-util-versioned-jar' target, which is at the bottom of the build.xml file shown above. It creates the 'build-version' directory in our project and then builds our versioned jar file based on the classes in the project's /bin directory. It is accessible to use in the Ant view for 'teamcakes-ant-util'. We can run the 'create-teamcakes-ant-util-versioned-jar' by double-clicking on the target in the Ant view.

Eclipse Ant View

If we run the 'create-teamcakes-ant-util-versioned-jar' target, we see the following result in the console:

Buildfile: C:\projects\workspace\teamcakes-ant-util\build.xml
create-teamcakes-ant-util-versioned-jar:
    [mkdir] Created dir: C:\projects\workspace\teamcakes-ant-util\build-version
jar-version:
      [jar] Building jar: C:\projects\workspace\teamcakes-ant-util\build-version\teamcakes-ant-util-1.0.jar
BUILD SUCCESSFUL
Total time: 844 milliseconds

After running the 'create-teamcakes-ant-util-versioned-jar' target, we see that the teamcakes-ant-util-1.0.jar file has been created in the build-version directory. In addition, a build.number file has been created in the root directory of our project that keeps track of the current 'minor version number' of our project.

teamcakes-ant-util-1.0.jar file has been created

Now we have a jar file that we can use to package the Variable class and any other classes that we're interested in using when we create custom Ant tasks. I'll add the jar file to a directory called 'ant-jars' that I use to store the various jar files that I utilize with Ant.

ant-jars directory in Eclipse Navigator

I'll go to Windows → Preferences → Ant → Runtime and add 'teamcakes-ant-util-1.0.jar' to my Ant classpathW, as shown below.

adding 'teamcakes-ant-util-1.0.jar' to Ant classpath

Since our jar file is now in our Ant classpath, we can reference the Variable class in our build.xml files. I define the task in my build-main.xml file using the taskdef element. I give the task the name 'var' and specify that 'var' maps to the 'com.cakes.ant.Variable' class.

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

After that, I copy my 'variable-test' target from my teamcakes-ant-util project's build.xml file to my ant-utilities project's build-main.xml file. This allows us to test the variable task from within ant-utilities.

	<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>

If I execute the 'variable-test' target in the ant-utilities project's build-main.xml file, we see the following result in the console, indicating that our 'var' task works correctly.

Buildfile: C:\projects\workspace\ant-utilities\build-main.xml
variable-test:
      [var] Setting 'myVar' variable to 'value1'
     [echo] myVar is value1
      [var] Setting 'myVar' variable to 'value2'
     [echo] myVar is value2
BUILD SUCCESSFUL
Total time: 469 milliseconds

Easy, huh? Customized tasks are a great way to allow Ant to call advanced functionality within JavaSW. As you can see, if you can't find an Ant task to do something that you need it to do, you can very easily write your own task.

(Continued on page 3)

Page: < 1 2 3 >