Reputation: 23
writing your own task should be a simple task. according to the documentation all you need is to extend org.apache.tools.ant.Task. the site provide a simple class example:
package com.mydomain;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class MyVeryOwnTask extends Task {
private String msg;
// The method executing the task
public void execute() throws BuildException {
System.out.println(msg);
}
// The setter for the "message" attribute
public void setMessage(String msg) {
this.msg = msg;
}
}
and in order to use it using the build.xml:
<?xml version="1.0"?>
<project name="OwnTaskExample" default="main" basedir=".">
<taskdef name="mytask" classname="com.mydomain.MyVeryOwnTask"/>
<target name="main">
<mytask message="Hello World! MyVeryOwnTask works!"/>
</target>
</project>
my question is, where should i put the MyVeryOwnTask.java file, should it be .jar file? should it be relative in some way to the build.xml file? com.mydomain.MyVeryOwnTask does it represents a file structure like a java project in eclipse?
my ant directory is C:\ant. i have all the environment vars set.
thanks.
Upvotes: 0
Views: 121
Reputation: 122364
The best approach is to put it in a jar file and then add a <classpath>
inside the <taskdef>
pointing to the jar:
<taskdef name="mytask" classname="com.mydomain.MyVeryOwnTask">
<classpath>
<fileset file="mytask.jar"/>
</classpath>
</taskdef>
You can put the jar in ant's own lib directory instead, and then you don't need the classpath, but using an explicit classpath element as above means your build file will work with a standard unmodified ant installation so it's a good habit to get into particularly if you are going to be collaborating with other developers.
Upvotes: 2