Java build tool-ant Application Guide (3)
Call ant in your own program
Ant's task is actually a piece of functional code. A large number of built-in tasks of ant are of great significance for developing Java applications. Why cannot we use them directly?
Although it is not complicated to call ant tasks in a program, we know that ant tasks are actually Java classes, and the called methods are nothing more than executing these classes, however, Initialization is required before execution. Therefore, we need to reference a subclass of the task class to implement this function, such as the following code:
Package com. sharetop. antdemo; Import org. Apache. Tools. Ant .*; Import org. Apache. Tools. Ant. taskdefs .*; Import java. Io. file; Public class runanttask { Public runanttask (){ } Public static void main (string ARGs []) { Antjar J = new antjar (); J. setbasedir (new file ("./classes ")); J. setjarfile (new file ("AAA. Jar ")); J.exe cute (); } } Final class antjar extends jar { Public antjar (){ Project = new project (); Project. INIT (); Tasktype = "jar "; Taskname = "jar "; } } |
Note that the antjar class constructor first creates a project and initializes it. This is a required condition for directly calling a task.
To execute ant in your own program, you need to understand several buildevent defined by ant, including:
◆ Build started
◆ Build finished
◆ Target started
◆ Target finished
◆ Task started
◆ Task finished
◆ Message logged
What we need to do is implement the buildlistener interface to handle various events, and the method for executing ant is very similar to the above example. Take some codes of the actually developed antbuilder software as an example:
Public void buildtarget (string targetname, string buildfilename ){ Try { Project P = new project (); P. INIT (); File F = new file (buildfilename ); P. setuserproperty ("ant. File", F. getabsolutepath ()); Projecthelper. configureproject (p, F ); P. addbuildlistener (this ); If (targetname = NULL) P.exe cutetarget (P. getdefaulttarget ()); Else P.exe cutetarget (targetname ); } Catch (exception ex ){ Jtextarea1.append (ex. getmessage ()); } } |
Create a project and initialize it, and set its configuration file (build. XML), execute the default or specified target, and then process the events you are interested in the listener class that implements the buildlistenser interface. The Code is as follows:
Public void buildstarted (buildevent event) {/* nothing */} Public void buildfinished (buildevent event) {/* nothing */} Public void targetstarted (buildevent event ){ This. jtextarea1.append (event. gettarget (). getname () + ":/n/R "); } Public void targetfinished (buildevent event) {/* nothing */} Public void taskstarted (buildevent event) {/* nothing */} Public void taskfinished (buildevent event) {/* nothing */} Public void messagelogged (buildevent event ){ Int prior = event. getpriority (); Switch (prior ){ Case Project. msg_err: This. jtextarea1.append ("[" + event. gettask (). gettaskname () + "] err :" + Event. getmessage ()); Break; Case Project. msg_info: This. jtextarea1.append ("[" + event. gettask (). gettaskname () + "]" + event. getmessage ()); Break; Case Project. msg_warn: This. jtextarea1.append ("[" + event. gettask (). gettaskname () + "]" + Event. getmessage ()); Break; Case Project. msg_verbose: This. jtextarea1.append (event. getmessage ()); Break; } } |
The build. xml file is written differently in every company. There is not much reference value here, so I will skip it. (Full text)