Monday, September 27, 2010

Write a custom task, easily extend Ant


Ant comes with a large number of tasks (Task), the Internet is also a large number of tasks available, but if you are facing the problems others will not mind, how should we do? In fact, if you can spend a little effort to write out their tasks. I started also did not expect such a simple (do nested tasks encountered a little problem), we can see that Ant's structural design is quite good.

Suppose we need to show that the implementation of the task and how much time to write a TimerTask

Start: the execute method of covering org.apache.tools.ant.Task
import org.apache.tools.ant.Task;

import org.apache.tools.ant.BuildException;

public class TimerTask extends Task (

public void execute () throws BuildException (

System.out.println ("I am a timer");

)

)

Note that when compiling Classpath on ant.jar must.

Simplicity, assume that java source files, class files in the same directory, in this directory to write a build.xml file:















Open Dos window to this directory, run ant

Simple enough right! If the class path is more complex or in the jar file, set the classpath attribute on the line.

Continue to: set properties

Suppose we want to add an action attribute, 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 the intermediate results using the Project

For this task, it must value the time saved, more appropriate is org.apache.tools.ant.Project a, Project object life cycle is the Build, and can be setProperty, getProperty methods such as save / get value.

public void execute () throws BuildException (

if (getOwningTarget () == null)




[Next]



return;

Project proj = getOwningTarget (). GetProject ();

...



/ / Can do smt with proj now

)

Promotion: Nested Task

If we want to re-nested in the Timer the following sub-tasks (if not necessary :-):







That we first of all just write a simple FooTask, and then add in the build.xml definition:





Then add a method to TimerTask:

public void addFoo (FooTask foo) (

)

Note: addXXX of XXX in the name decided by the , Ant will use Java Reflection to find; type must be concrete type FooTask, can not be Task.






Recommended links:



New Audio Video Tools



Gateway marrying the truth in China



New Economy Weekly cover story: the role of Angel Zhou Hongyi and more



Fully compatible with and support the collection of Thunder Win7 new trial



Red Hat Reports Fourth-quarter earnings fell 25% year on year profit



Bargain obsolete HP Computers doubts



XviD to WMV



Art - Screen Savers Shop



E-cology in the Pan Micro Series 29



manage your contacts 2



MKV to Zune



Lists System Maintenance



Availability of the indicator, the user experience is the goal, UCD is thought



Expert Audio Video Tools



THUNDER turned to commercial content channels: forced out of the exploration



AVI to DivX



No comments:

Post a Comment