Wednesday, September 29, 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:



Top File Compression



Associate



Registry Tools Storage



Zack Rusin interviewed the beauty and magic OF KDE



AMD Intel on EU antitrust penalties comment



DV To AVI



Watch cool mechanical PICTURE production process



Window frames with good HTML tag



Photoshop font effects - Ice Text



Liang No. 400 400 640 074 008 Take your Pick



VOB To FLV



Wizard Games And ENTERTAINMENT



brief E-Mail Tools



Thought to help in the day to enjoy from under The rising voices



DAT To MPEG



No comments:

Post a Comment