Write a custom task and easy to expand Ant
Ant comes with a lot of tasks (Task), there is a lot of tasks on the Internet, but if you face the problem that others will not think of, what should I do? In fact, you can write your own task as long as you spend a little hard. I didn't expect to be simple (a nesting task to encounter a problem), which can be seen that the structural design of Ant is quite excellent.
Suppose we need to show how much time spent on the task, write a Timertask
Start: Covering org.Apache.Tools.ant.Task Execute Method
Import org.apache.tools.ant.task;
Import org.apache.tools.nt.buildexception;
Public class timertask extends task {
Public void execute () throws buildexception {
System.out.println ("I am a Timer");
}
}
Note that Ant.jar must be on ClassPath upon compile.
Simple, assume the Java source file, class files in the same directory, write a build.xml file in this directory:
Name = "Testtimer" default = "beed" based "based" based ","
Name =
"Timer"
ClassName =
"TIMERTASK"
Classpath =
"."
/>
Name =
"TEST"
>
/>
Open the DOS window, to this directory, run Ant
Simple enough! Settings if the class path is complicated than this or in the JAR file
The ClassPath property is OK.
Continue: Setting properties
Suppose we want to add an action property in the Java file:
PRIVATE STRING ACTION;
.
Public void execute () throws buildexception {
IF (action.equals ("init")) {
...
}
...
}
Public String getAction () {
Return Action;
}
Public void setAction (String string) {
Action = String;
}
In the XML file:
Save intermediate results using Project
For this task, the time value must be saved, and the appropriate is org.apache.tools.ant.project, the Project object life is the entire building, and the / acquisition value can be saved by SETPROPERTY, GETPROPERTY.
Public void execute () throws buildexception {
IF (getowningTarget () == null)
Return;
Project Proj = getownTarget (). GetProject ();
...
// can do SMT with Proj Now
}
Advance: Nested task
If we want to nest in Timer (see if this is necessary :-):
Then let's first need to write a simple footask, then in build.xml, the definition:
Name = "Timer" classname = "TIMERTASK" classpath = "." />
Name = "foo" classname = "Footask" classpath = "." />
Then add a method to Timertask:
Public void addfoo (footask foo) {
}
Note: AddXXX XXX
Name decision, Ant will use Java Reflection to find it; the type must be a specific type FOOTASK, can't be Task.