Here is an admittedly ugly class that I whipped together once. You will want to clean 
it up and customize it for your purposes, but it should get you started. 

--Dan

import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.BuildLogger;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.taskdefs.Echo;

import java.io.File;
import java.io.PrintStream;
import java.io.ByteArrayOutputStream;
import java.util.Vector;
import java.util.HashMap;
import java.util.Iterator;

public class  AntLauncher
{
        
     public static String run( HashMap args ) 
     { 
          final Project project = new Project();
          ByteArrayOutputStream out = null;
          
          try
          {
               out = new ByteArrayOutputStream();
               project.addBuildListener(AntLauncher.createLogger( out )); 
               project.init(); 
               File buildFile = new File( (String)args.remove("ant.file") );
               ProjectHelper.configureProject(project, buildFile);
               String path = buildFile.getAbsolutePath();
               
               // Set Build File
               project.setUserProperty("ant.file", path);
               
               // Set runtime properties
               Iterator iterator = args.keySet().iterator();
               while ( iterator.hasNext() )
               {
                    String key = (String)iterator.next();
                    String value = (String)args.get( key );
                    project.setUserProperty( key, value );    
               }
               
               Vector targets = new Vector(5); 
               Target init = new Target();
               init.setName("echo");
               init.setProject(project);
               String myTarget = project.getDefaultTarget();
               targets.addElement( myTarget );
               Echo  echo = new Echo();
               echo.setMessage("Trying to Run ant using java.");
               init.addTask(echo);
               project.addTarget(init);
               project.executeTargets(targets);
          } 
          catch (RuntimeException exc ) 
          {
               exc.printStackTrace();
          } 
          
          return out.toString();
     }
        
     /**
     *  Creates the default build logger for sending build events to the ant log.
     */
     private static BuildLogger createLogger( ByteArrayOutputStream out )
     {
          BuildLogger logger = null;
          logger = new DefaultLogger();

          logger.setMessageOutputLevel(Project.MSG_INFO);
          logger.setOutputPrintStream(new PrintStream( out ));
          logger.setErrorPrintStream(new PrintStream( out ));
          logger.setEmacsMode(false);

          return logger;
     }
}




-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, September 17, 2002 6:53 AM
To: [EMAIL PROTECTED]
Subject: Running ant from Java


Hi

Has anyone written a class that has a method that can run Ant from a Java class. The 
methods in org.apache.tools.ant.Main all calls "System.exit()" and hence cannot be 
used for this purpose.

/G�ran

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


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

Reply via email to