April 2005
Intermediate to advanced
336 pages
7h 20m
English
You can handle build events with custom loggers as well if
you extend the Ant DefaultLogger
class. Example 12-17 shows a
logger that will log each task as it's executed. Like listeners, loggers
must not access System.out and
System.err directly, so to display
messages to the standard out device, this code uses the Ant log( ) method (which defaults to standard
out).
Example 12-17. A new logger (ch12/logger/ProjectLogger.java)
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.util.StringUtils;
public class ProjectLogger extends DefaultLogger
{
public void taskStarted(BuildEvent event)
{
String text = "Running task " + event.getTask( ).getTaskName( )
+ StringUtils.LINE_SEP;
printMessage(text, out, event.getPriority( ));
log(text);
}
}For this example, use a simple custom task:
public class Project
{
public void execute( )
{
System.out.println("No worries.");
}
}Here's the build file:
<?xml version="1.0"?>
<project basedir="." default="main">
<property name="src" value="src"/>
<property name="output" value="output"/>
<target name="main" depends="jar">
<taskdef name="project" classname="Project" classpath="Project.jar"/>
<project/>
</target>
<target name="compile">
<mkdir dir="${output}"/>
<javac srcdir="${src}" destdir="${output}"/>
</target>
<target name="jar" depends="compile">
<jar destfile="Project.jar" basedir="${output}"/>
</target>
</project>When you execute this build file with the custom ...
Read now
Unlock full access