Listing  1   Example buildfile: Build.xml

<project
name="CMP11" default="do_all" basedir=".">

<!-- set up project properties -->
<property name="src_Dir" value="."/>
<property
  name="bld_Dir" value="${src_Dir}/build"/>
<property name="jar_Dir" value="${src_Dir}/jars"/>
<property name="appName" value="cmp11"/>
<property name="appServer" value="Default Server"/>
<property name="nodeName" value="IBM-2OMCG4EIXUG"/>
<property
  name="hdDir"
  value="${WAS_HOME}/installedApps/CmpApp.ear"/>
<property environment="system"/>

<target
  name="do_all"
  depends="cleanUp, init, preDeploy_ejb, ejbdeploy, hotDeploy"/>

<!-- Remove the build directory left from old build -->
<target name="cleanUp">
  <delete dir="${bld_Dir}"/>
  <delete dir="${jar_Dir}"/>
</target>

<!-- Create the build directory structure used by compile -->
<!-- and copy the deployment descriptors into it-->
<target name="init">
  <mkdir dir="${bld_Dir}"/>
  <mkdir dir="${bld_Dir}/META-INF"/>
  <mkdir dir="${jar_Dir}"/>
  <copy todir="${bld_Dir}/META-INF">
  <fileset
   dir="${src_Dir}/META-INF"></fileset>
  </copy>
</target>

<!-- Prepares source files for deployment: -->
<!-- compiles ejb source into build directory, -->
<!-- jars the compiled classes -->
<target name="preDeploy_ejb" depends="init">
  <javac
  srcdir="${src_Dir}"
  destdir="${bld_Dir}"
  extdirs="${system.WAS_EXT_DIRS}"/>
  <jar
  jarfile="${jar_Dir}/${appName}.jar"
  basedir="${bld_Dir}"></jar>
</target>


<!-- Generate deployed code for ejbs via ejbDeploy -->
<target
  name="ejbdeploy" depends="preDeploy_ejb">
  <exec
  executable="ejbdeploy.bat"
  os="Windows NT, Windows 2000">
  <arg
line="${jar_Dir}/${appName}.jar c:\temp
${jar_Dir}/Deployed_${appName}.jar">
  </arg>
  </exec>
  <exec
  executable="ejbdeploy.sh"
  os="AIX, SunOS, HP-UX, Linux">
  <arg
  line="${jar_Dir}/${appName}.jar /tmp
   ${jar_Dir}/Deployed_${appName}.jar">
   </arg>
  </exec>
</target>

<!-- Copy the deployed jar to the -->
<!-- WS installed app dir for hot deploy -->
<target name="hotDeploy">
  <stopWSAppServer
  nodeName="${nodeName}"
  appServerName="${appServer}"/>
  <copy
  file="${jar_Dir}/Deployed_${appName}.jar"
  tofile="${hdDir}/${appName}.jar"/>
  <startWSAppServer
  nodeName="${nodeName}"
  appServerName="${appServer}"/>
</target>

</project>


Listing  2   Example WebSphere Application Server custom task


package org.apache.tools.ant.taskdefs.optional.ejb;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Execute;
import java.io.*;

// A custom task needs to extend the
// org.apache.tools.ant.Task class.
public class StopWSAppServer extends Task {
String nodeName = "";
String appServerName = "";

// Setters for the two attributes:
//     nodeName and AppServerName
public void setNodeName(String arg) {
  nodeName = arg;
}
public void setAppServerName(String arg) {
  appServerName = arg;
}

// Do the work
public void execute() throws BuildException {
  if (nodeName.length() == 0) {
  System.out.println(
   "Missing the Node Name. "
    + "Please specify the name of the node "
    + "the application server resides on.");
  throw new BuildException(
       "StopWSAppServer failed. Missing Node Name.");
  }
  if (appServerName.length() == 0) {
  System.out.println(
   "Missing the Application Server Name. "
    + "Please specify the name of the application"
    + "server you want to stop.");
  throw new BuildException(
   "StopWSAppServer failed. "
    + "Missing Application Server Name.");
  }

  StringBuffer appServer =
  new StringBuffer("ApplicationServer stop {/Node:");
  appServer.append(nodeName);
  appServer.append("/ApplicationServer:");
  appServer.append(appServerName);
  appServer.append("/}");

  String[] command = new String[3];
  String osName = System.getProperty("os.name");
  if (osName.indexOf("Windows") >= 0)
  command[0] = "wscp.bat";
  else
  command[0] = "wscp.sh";
  command[1] = "-c";
  command[2] = appServer.toString();

  try {
  Execute.runCommand(this, command);
  } catch (Exception e) {
  throw new BuildException(
   "StopWSAppServer task failed. "
    + "Could not execute command: "
    + command[0]
    + " -c "
    + command[2]
    + e);
  }
}
}


Listing  3   Example Ant BuildListener


package com.ibm.ant.example.buildlistener;

import org.apache.tools.ant.*;
import java.io.*;

// A custom listener needs to implement the
// org.apache.tools.ant.BuildListener interface
public class MailSenderBL
  implements org.apache.tools.ant.BuildListener {

  public MailSenderBL() {
  }
 
  public void buildStarted(BuildEvent e) {
     System.out.println(
     "Build Started: " + e.getProject().getName());
  }

  public void buildFinished(BuildEvent e) {
   System.out.println(
     "Build Finished: " + e.getProject().getName());
   System.out.println("Sending mail ....");
  
   // Try to send a mail message
   try {
     org.apache.tools.mail.MailMessage msg =
       new org.apache.tools.mail.MailMessage(
         "<INSERT STMP SERVER NAME HERE>");
     msg.from("build@mycompany.com");
     msg.to("sarah@mycompany.com");
     msg.setSubject("Ant Build Complete");
     PrintStream out = msg.getPrintStream();
     out.println("Build is Complete.");
     msg.sendAndClose();
   } catch (Exception excpt) {
     System.out.println(
       "Could not mail message ..." + excpt.toString());
   }
  }

  public void targetStarted(BuildEvent e) {
   System.out.println(
     "Target Started: " + e.getTarget().getName());
  }

  public void targetFinished(BuildEvent e) {
   System.out.println(
     "Target Finished: " + e.getTarget().getName());
  }

  public void taskStarted(BuildEvent e) {
   System.out.println(
     "Task Started: " + e.getTask().getTaskName());
  }

  public void taskFinished(BuildEvent e) {
   System.out.println(
     "Task Finished: " + e.getTask().getTaskName());
  }

  public void messageLogged(BuildEvent e) {
  }

}