Return-Path: Delivered-To: apmail-ant-user-archive@www.apache.org Received: (qmail 73415 invoked from network); 30 Aug 2004 18:34:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 30 Aug 2004 18:34:32 -0000 Received: (qmail 62934 invoked by uid 500); 30 Aug 2004 18:34:16 -0000 Delivered-To: apmail-ant-user-archive@ant.apache.org Received: (qmail 62881 invoked by uid 500); 30 Aug 2004 18:34:16 -0000 Mailing-List: contact user-help@ant.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Ant Users List" Reply-To: "Ant Users List" Delivered-To: mailing list user@ant.apache.org Received: (qmail 62867 invoked by uid 99); 30 Aug 2004 18:34:16 -0000 X-ASF-Spam-Status: No, hits=2.8 required=10.0 tests=DNS_FROM_RFC_ABUSE,DNS_FROM_RFC_POST,RCVD_IN_RFC_IPWHOIS X-Spam-Check-By: apache.org Received-SPF: pass (hermes.apache.org: local policy) Received: from [192.193.226.97] (HELO mail.citigroup.com) (192.193.226.97) by apache.org (qpsmtpd/0.27.1) with ESMTP; Mon, 30 Aug 2004 11:34:15 -0700 Received: from imbarc-ny02.ny.ssmb.com (imbarc-ny02.ny.ssmb.com [162.124.186.139]) by imbaspam-ss01.namdmz.dmzroot.net (8.12.10/8.12.10/SSMB_EXT/ev: 10651 $) with ESMTP id i7UIYDYP016365 for ; Mon, 30 Aug 2004 18:34:13 GMT Received: from mailhub-nyc3.ny.ssmb.com (mailhub-nyc3-hme0.ny.ssmb.com [162.124.148.17]) by imbarc-ny02.ny.ssmb.com (8.12.9/8.12.9/SSMB_QQQ_IN/1.1) with ESMTP id i7UIXvFb027569 for ; Mon, 30 Aug 2004 18:33:57 GMT Received: from exnjsm04.nam.nsroot.net (EXNJSM04.nam.nsroot.net [169.193.40.38]) by mailhub-nyc3.ny.ssmb.com (8.12.10/8.12.10/CG_HUB) with ESMTP id i7UIXXpH029773 for ; Mon, 30 Aug 2004 18:33:54 GMT Received: from exnjmb30.nam.nsroot.net ([169.193.40.32]) by exnjsm04.nam.nsroot.net with Microsoft SMTPSVC(5.0.2195.6713); Mon, 30 Aug 2004 14:33:51 -0400 X-MimeOLE: Produced By Microsoft Exchange V6.0.6487.1 content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: RE: calling ant from java program Date: Mon, 30 Aug 2004 14:33:50 -0400 Message-ID: <81E189F07E3537408C1AE7BA1C93FD3F01CDCBCF@EXNJMB30.nam.nsroot.net> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: calling ant from java program Thread-Index: AcSOviZvbZ2/DLztRiW8tu21MHdDpAAAAOVg From: "Velagapudi, Murali" To: "Ant Users List" X-OriginalArrivalTime: 30 Aug 2004 18:33:51.0180 (UTC) FILETIME=[E56648C0:01C48EBF] X-Scanned-By: MIMEDefang 2.36 X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Hi, Antoine thx for reply,i had kept both commons-net.jar and = jakarta-oro.jar files in ant/lib=20 ,but when i call from java program it does not work(it works if = build.xml didn't have any optional task) when i run from command it works well and send the file, but the problem is when i run this using the below java program it is = not able to recognize ftp task=20 (not only ftp also telnet,script etc say optional tasks) it says could not find ftp task i tried to run the program using=20 java -cp commons-net.jar;jakarta-oro.jar ..but still it is not taking = up.. is there any thing i need to keep in class path ?. here is the snippet i have been working on =20 =20 =20 =20 import org.apache.tools.ant.*; import java.io.*; import java.util.*; public class AntRunner { private Project project; public void init(String _buildFile, String _baseDir) throws Exception { project =3D new Project(); try { project.init(); } catch (BuildException e) { throw new Exception("The default task list could not be = loaded."); } // Set the base directory. If none is given, "." is used. if (_baseDir =3D=3D null) _baseDir=3Dnew String("."); try { project.setBasedir(_baseDir); } catch (BuildException e) { throw new Exception("The given basedir doesn't exist, or = isn't a directory."); } =20 if (_buildFile =3D=3D null) _buildFile=3Dnew = String("build.xml"); try { ProjectHelper.getProjectHelper().parse(project, new File(_buildFile)); } catch (BuildException e) { throw new Exception("Configuration file "+_buildFile+" is = invalid, or cannot be read."); } } public void runTarget(String _target) throws Exception { // Test if the project exists if (project =3D=3D null) throw new Exception("No target can be = launched because the project has not been initialized. Please call the = 'init' method first !"); // If no target is specified, run the default one. if (_target =3D=3D null) _target =3D = project.getDefaultTarget(); // Run the target try { project.executeTarget(_target); } catch (BuildException e) { throw new Exception(e.getMessage()); } } public static void main(String args[]) { try{ =09 AntRunner ar=3Dnew AntRunner(); =09 ar.init("build.xml","."); =09 ar.runTarget(null); }catch(Exception e) { e.printStackTrace(); } } } thanks Murali -----Original Message----- From: Antoine Levy-Lambert [mailto:antoine@gmx.de] Sent: Monday, August 30, 2004 2:20 PM To: Ant Users List Subject: RE: calling ant from java program Hi, to run optional tasks, you need the corresponding ant-xyz jar files in = the classpath, plus also the corresponding non ant jar files. For instance for ftp, you need commons-net and jakarta-oro. This is all documentented in the manual, in the section installing ant, library dependencies. Cheers, Antoine >=20 >=20 > I am able to run ant from java program with the way mentioned by = you > ,but if there are any optional tasks > it is giving the errors or "could not create task" for that optional > task,where as if run the same build.xml with=20 > optional task using ant command it executes very well, the problem > persists only if i am calling that java program > and that too it should have optional task,here am i missing any = classpath > or any some thing like that > i even tried with executing java -cp option but still it doesn't work = ,any > help would be great. >=20 > thanks, > Murali >=20 > -----Original Message----- > From: Olivier Croisier [mailto:Olivier.Croisier@thales-is.com] > Sent: Wednesday, August 25, 2004 11:44 AM > To: Ant Users List > Subject: Re: calling ant from java program >=20 >=20 > > How can I call a ant task from java program? > > Regds > > Ashutosh >=20 >=20 > Here is some code I wrote. > It allows you to run any ant target from within a regular Java app. > You'll need ant-launcher.jar and ant.jar for this class to compile. > Enjoy :) >=20 > BTW, any comment/idea/debug/fix welcome ! >=20 >=20 >=20 > import org.apache.tools.ant.*; >=20 > import java.io.*; > import java.util.*; >=20 > /** > *
>   *   This class is designed to call Ant targets from any Java
> application.
>   *   1. Initialize a new Project by calling "init"
>   *   2. Feed Ant with some properties by calling "setProperties"
> (optional)
>   *   3. Run an Ant target by calling "runTarget"
>   *
>   *
>   *   Example :
>   *
>   *   try
>   *   {
>   *       //init
>   *       init("/home/me/build.xml","/home/me/");
>   *       //properties
>   *       HashMap m =3D new HashMap();
>   *       m.put("event", "test");
>   *       m.put("subject", "sujet java 3");
>   *       m.put("message", "message java 3");
>   *       setProperties(m, false);
>   *       //run
>   *       runTarget("test");
>   *   } catch (Exception e) { e.printStackTrace(); }
>   * 
> * > * @author croisier > */ >=20 >=20 > public class AntRunner > { > private Project project; >=20 >=20 > /** > * Initializes a new Ant Project. > * @param _buildFile The build File to use. If none is provided, = it > will be defaulted to "build.xml". > * @param _baseDir The project's base directory. If none is = provided, > will be defaulted to "." (the current directory). > * @throws Exception Exceptions are self-explanatory (read their > Message) > */ > public void init(String _buildFile, String _baseDir) throws = Exception > { > // Create a new project, and perform some default = initialization > project =3D new Project(); > try { project.init(); } > catch (BuildException e) > { throw new Exception("The default task list could not be > loaded."); } >=20 > // Set the base directory. If none is given, "." is used. > if (_baseDir =3D=3D null) _baseDir=3Dnew String("."); > try { project.setBasedir(_baseDir); } > catch (BuildException e) > { throw new Exception("The given basedir doesn't exist, = or > isn't a directory."); } >=20 > // Parse the given buildfile. If none is given, "build.xml" = is > used. > if (_buildFile =3D=3D null) _buildFile=3Dnew = String("build.xml"); > try { ProjectHelper.getProjectHelper().parse(project, new > File(_buildFile)); } > catch (BuildException e) > { throw new Exception("Configuration file "+_buildFile+" = is > invalid, or cannot be read."); } > } >=20 >=20 >=20 > /** > * Sets the project's properties. > * May be called to set project-wide properties, or just before a > target call to set target-related properties only. > * @param _properties A map containing the properties' name/value > couples > * @param _overridable If set, the provided properties values may = be > overriden by the config file's values > * @throws Exception Exceptions are self-explanatory (read their > Message) > */ > public void setProperties(Map _properties, boolean _overridable) > throws Exception > { > // Test if the project exists > if (project =3D=3D null) throw new Exception("Properties = cannot be > set because the project has not been initialized. Please call the = 'init' > method first !"); >=20 > // Property hashmap is null > if (_properties =3D=3D null) throw new Exception("The = provided > property map is null."); >=20 > // Loop through the property map > Set propertyNames =3D _properties.keySet(); > Iterator iter =3D propertyNames.iterator(); > while (iter.hasNext()) > { > // Get the property's name and value > String propertyName =3D (String) iter.next(); > String propertyValue =3D (String) > _properties.get(propertyName); > if (propertyValue =3D=3D null) continue; >=20 > // Set the properties > if (_overridable) project.setProperty(propertyName, > propertyValue); > else project.setUserProperty(propertyName, = propertyValue); > } > } >=20 >=20 >=20 > /** > * Runs the given Target. > * @param _target The name of the target to run. If null, the > project's default target will be used. > * @throws Exception Exceptions are self-explanatory (read their > Message) > */ > public void runTarget(String _target) throws Exception > { > // Test if the project exists > if (project =3D=3D null) throw new Exception("No target can = be > launched because the project has not been initialized. Please call the 'init' > method first !"); >=20 > // If no target is specified, run the default one. > if (_target =3D=3D null) _target =3D = project.getDefaultTarget(); >=20 > // Run the target > try { project.executeTarget(_target); } > catch (BuildException e) > { throw new Exception(e.getMessage()); } > } >=20 > } >=20 --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@ant.apache.org For additional commands, e-mail: user-help@ant.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@ant.apache.org For additional commands, e-mail: user-help@ant.apache.org