ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniel L. Boxwell" <Boxwel...@C-IV.org>
Subject RE: Running ant from Java
Date Tue, 17 Sep 2002 14:41:31 GMT
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: Ext-Goran.Westin@nokia.com [mailto:Ext-Goran.Westin@nokia.com]
Sent: Tuesday, September 17, 2002 6:53 AM
To: ant-user@jakarta.apache.org
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:ant-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-user-help@jakarta.apache.org>


--
To unsubscribe, e-mail:   <mailto:ant-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-user-help@jakarta.apache.org>


Mime
View raw message