What classpath do I need for Ant taskdef?

I am new to Ant. Can someone please tell me what value should be used for classpathref for taskdef? Will this be the path to the class file? If so, an example may be given because I tried this and did not work for me.

+5
source share
1 answer

In taskdef, the value classpathrefmust be a reference to a previously defined one path. The path must include a jar archive that contains the class that implements the task, or it must point to a directory in the file system that is the root of the class hierarchy. This will not be the actual directory that your class contains if your class is in the package.

Here is an example.

MyTask.java:

package com.x.y.z;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class MyTask extends Task
{
    // The method executing the task
    public void execute() throws BuildException {
        System.out.println( "MyTask is running" );
    }
}

, com.x.y.z, - , classes - :

$ ls classes/com/x/y/z
MyTask.class

build.xml, :

<project name="MyProject" basedir=".">

<path id="my.classes">
    <pathelement path="${basedir}/classes" />
</path>
<taskdef name="mytask" classpathref="my.classes" classname="com.x.y.z.MyTask"/>
<mytask />

</project>

, classpathref classes - .

:

$ ant
Buildfile: .../build.xml
   [mytask] MyTask is running

classpath, classpathref, :

<property name="my.classes" value="${basedir}/classes" />
<taskdef name="mytask" classpath="${my.classes}" classname="com.x.y.z.MyTask"/>
+5

All Articles