In the taskdef, the classpathref
should be a reference to a previously defined path
.
The path should include a jar archive that holds the class implementing the task,
or it should point to the directory in the file system that is the root of the class hierarchy.
This would not be the actual directory that holds your class if your class resides in a package.
这是一个例子。
我的任务.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" );
}
}
Note that the package name is com.x.y.z
, so when deployed -
lets say the classes are put under a directory called classes
- we might see the class here in the file system:
$ 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
给定的点位于class
目录——类层次结构的根目录。
运行时,我们得到:
$ ant
Buildfile: .../build.xml
[mytask] MyTask is running
您可以使用显式<code>classpath</code>而不是classpathref来执行类似操作,例如:
<property name="my.classes" value="${basedir}/classes" />
<taskdef name="mytask" classpath="${my.classes}" classname="com.x.y.z.MyTask"/>