Java 2EE and XML Development

Скачать в pdf «Java 2EE and XML Development»


import java.util.*;


Implements the BuildListener callback interface


import org.apache.tools.ant.*;


public class BuildResultPrinter implements BuildListener {    <


private Vector buildMessages = new Vector();


Writes all logged messages to a file named


{    build.results



succeeded»;


public BuildResultPrinter() { }


public void buildStarted(BuildEvent e) { }


public void buildFinished(BuildEvent event) Throwable e = event.getException();


String status = (e != null) ? «failed» : String message = «Your Ant build process + status + «.»;


try {


PrintStream ps = new PrintStream(


new FileOutputStream(«build.results») );


ps.println(message); if (buildMessages.size() > 0)    {


ps.println(); ps.println(


«The following messages were logged by Ant:»); ps.println(


«——————————————«);


ps.println();


for (int i = 0; i < buildMessages.size(); i++) ps.println( (String) buildMessages.get(i) );


}


ps.close();


} catch (Exception ex) { ex.printStackTrace();



}


}


public void messageLogged(BuildEvent event) { buildMessages.add( event.getMessage() );



}



Collects messages as they occur and caches them



public void targetStarted(BuildEvent event) { }



public void targetFinished(BuildEvent event) { } public void taskStarted(BuildEvent event) { }



public void taskFinished(BuildEvent event) { }


}


Execute the build.listener task in the example build file to compile and JAR this class and place it in Ant’s classpath.

C.4.2    Using the listener


To put our new listener to work, execute the following in your working directory:


prompt> ant -listener BuildResultPrinter


You will now find a file named build.results in your working directory,
containing all logging messages created by Ant as it evaluated the run target.

Скачать в pdf «Java 2EE and XML Development»