Here's a short example, which shows how I did it. Used extension, but that's
not really needed. Only trick is to new the ANT project so you don't get an
error when the task does its logging. The ArgsParser class is a Java
GNU-getopts-like command line argument parser (not included!). Besides
showing how to call an ANT task from Java directly, it also shows how one
can provide direct access to the task functionality independently of the XML
build file. When thru this exercise because a colleague asked for this
functionality, but did tell him he should use ANT normally (with a XML build
file) instead.

Anyways, hope this helps. --DD


package com.lgc.jax.tools;

import java.io.File;
import java.io.PrintStream;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Copydir;

import com.lgc.jax.util.ArgsParser;
import com.lgc.jax.util.ArgsParser.OptionException;

/**
 * Copies a directory tree from the source to the destination
 * <p>
 * <em>requires ant.jar in the classpath!</em>
 *
 * @author <a href="mailto:[EMAIL PROTECTED]";>Dominique Devienne</a>
 */
public class CopydirX extends Copydir {

  { project = new Project(); } // Avoids NullPointerException during
logging!

  private static void printUsageAndExit(int exitCode) {
    PrintStream out = (exitCode==0)? System.out: System.err;
    out.println("Usage: java "+CopydirX.class.getName());
    out.println("  -s source -d destination [-i includes] [-e excludes]");
    out.print  ("  [--src=dir1] [-dest==dir2] ");
    out.println("[-includes=pattern1] [--excludes=pattern2]");
    System.exit(exitCode);
  }

  public static void execute(File src, File dest,
                             String includes, String excludes)
                     throws BuildException {
    if (src==null || dest==null) {
      throw new IllegalArgumentException("null src or dest");
    }

    CopydirX task = new CopydirX();

    task.setSrc(src);
    task.setDest(dest);

    if (includes!=null) {
      task.setIncludes(includes);
    }
    if (excludes!=null) {
      task.setExcludes(excludes);
    }

    task.execute();
  }

  public static void main(String[] args) 
                     throws BuildException, OptionException  {

    File src=null, dest=null;
    String includes=null, excludes=null;

    String shortOpts = "hs:d:i:e:";
    String[] longOpts = {"help","src=","dst=","includes=","excludes="};
    ArgsParser ap = new ArgsParser(args,shortOpts,longOpts);
    String[] opts = ap.getOptions();
    String[] vals = ap.getValues();

    for (int i=0; i<opts.length; ++i) {
      String opt = opts[i];
      String val = vals[i];

      if (opt.equals("-h") || opt.equals("--help")) {
        printUsageAndExit(0);
      }
      else if (opt.equals("-s") || opt.equals("--src")) {
        src = new File(val);
      }
      else if (opt.equals("-d") || opt.equals("--dest")) {
        dest = new File(val);
      }
      else if (opt.equals("-i") || opt.equals("--includes")) {
        includes = val;
      }
      else if (opt.equals("-e") || opt.equals("--excludes")) {
        excludes = val;
      }
    }

    if (ap.getOtherArgs().length>0) {
      printUsageAndExit(-1);
    }

    execute(src,dest,includes,excludes);
  }

} // END class CopydirX

/*
** DOS session demonstrating the command line driver:
*****************************************************
P:\com_lgc_jax\lib\classes>set CLASSPATH=.;..\ext\ant-1.3.jar
P:\com_lgc_jax\lib\classes>rem replace | by / in the command line below
P:\com_lgc_jax\lib\classes>java com.lgc.jax.tools.CopydirX 
               -s ..\..\src -d deploy -i "**|*.java" -e "**|tools|**"
P:\com_lgc_jax\lib\classes>dir deploy\com\lgc\jax
08/07/01  02:40p                 3,391 AllClasses.java
08/07/01  02:40p                 2,739 com_lgc_jax.java
08/07/01  02:40p        <DIR>          io
08/07/01  02:40p        <DIR>          jni
08/07/01  02:40p        <DIR>          lang
08/07/01  02:40p        <DIR>          math
08/07/01  02:40p        <DIR>          mt
08/07/01  02:40p        <DIR>          util
              10 File(s)          6,130 bytes
P:\com_lgc_jax\lib\classes>dir ..\..\src\com\lgc\jax
08/06/01  04:37p                 3,391 AllClasses.java
08/06/01  05:53p                12,569 com_lgc_jax.dsp
08/06/01  05:52p                12,683 com_lgc_jax.dsp~
08/06/01  05:19p                 2,739 com_lgc_jax.java
08/07/01  11:47a        <DIR>          io
08/07/01  11:51a        <DIR>          jni
08/07/01  12:03p        <DIR>          lang
08/07/01  02:24p                11,733 makefile.nmk
07/31/01  04:53p        <DIR>          math
07/31/01  04:52p                   164 mssccprj.scc
07/31/01  04:53p        <DIR>          mt
08/07/01  02:40p        <DIR>          tools
08/07/01  12:05p        <DIR>          util
08/06/01  05:55p                    96 vssver.scc
              16 File(s)         43,375 bytes
P:\com_lgc_jax\lib\classes>
*/

 -----Original Message-----
From:   Justin Lawler [mailto:[EMAIL PROTECTED]] 
Sent:   Wednesday, March 06, 2002 10:10 AM
To:     Ant Users List
Subject:        executing ant task from outside ant

How would you go about executing an ant task from a java program rather than
from ant itself? Is there any examples of this or has anybody else done it?

thanks,

Justin

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to