| Sign In/My Account | View Cart |
Open Source Java: Ant
Pages: 1, 2, 3
As you can see, this file follows the same structure as the simple
example above. This time, however, the project will by default create
a jar file named LessSimple with the date appended. A
target which acts to clean up the build areas is included too.
<?xml version="1.0"?>
<project name="LessSimple" default="dist" basedir=".">
<!-- set global properties for this build -->
<property name="src" value="." />
<property name="build" value="build" />
<property name="dist" value="dist" />
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}" />
</target>
<target name="compile" depends="init">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" />
</target>
<target name="dist" depends="compile">
<!-- Create the ${dist}/lib directory -->
<mkdir dir="${dist}/lib" />
<!-- Put everything in ${build} into the LessSimple-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/LessSimple-${DSTAMP}.jar" basedir="${build}" />
</target>
<target name="clean">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
</project>
As mentioned previously, Ant can be extended with your existing Java skills in two different ways: through the build events mechanism and through adding new task objects.
As with other areas of the Java platform, Ant generates build events as it runs through the build process. BuildListener objects can be attached to Ant. The listener receives BuildEvent objects for the following events:
Here's an example listener class to get you started. It simply outputs a message when the build begins and ends.
public class MyBuildListener implements BuildListener
{
public void buildStarted( BuildEvent event )
{
System.out.println( event.getProject().getName() + ": Build started..." );
}
public void buildFinished( BuildEvent event )
{
System.out.println( event.getProject().getName() + ": Build finished..." );
}
public void targetStarted( BuildEvent event )
{
System.out.println( event.getTarget().getName() + ": Target started..." );
}
public void targetFinished( BuildEvent event )
{
System.out.println( event.getTarget().getName() + ": Target finished..." );
}
public void taskStarted( BuildEvent event )
{
System.out.println( event.getTask().getTaskName() + ": Task started..." );
}
public void taskFinished( BuildEvent event )
{
System.out.println( event.getTask().getTaskName() + ": Task finished..." );
}
public void messageLogged( BuildEvent event )
{
System.out.println( "A Message: " + event.getTask().getMessage() );
}
}
If you wish to attach a listener from the command line you may use
the -listener option. For example,
ant -listener org.apache.tools.ant.XmlLogger
This will run Ant with a listener which generates an XML representation of the build progress.