Return-Path: Delivered-To: apmail-ant-user-archive@www.apache.org Received: (qmail 32560 invoked from network); 12 Jan 2011 20:23:29 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 12 Jan 2011 20:23:29 -0000 Received: (qmail 14474 invoked by uid 500); 12 Jan 2011 20:23:29 -0000 Delivered-To: apmail-ant-user-archive@ant.apache.org Received: (qmail 14372 invoked by uid 500); 12 Jan 2011 20:23:28 -0000 Mailing-List: contact user-help@ant.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Ant Users List" Reply-To: "Ant Users List" Delivered-To: mailing list user@ant.apache.org Received: (qmail 14364 invoked by uid 99); 12 Jan 2011 20:23:28 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Jan 2011 20:23:28 +0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of antoine@gmx.de designates 213.165.64.23 as permitted sender) Received: from [213.165.64.23] (HELO mail.gmx.net) (213.165.64.23) by apache.org (qpsmtpd/0.29) with SMTP; Wed, 12 Jan 2011 20:23:20 +0000 Received: (qmail invoked by alias); 12 Jan 2011 20:22:58 -0000 Received: from pool-74-101-86-127.nycmny.east.verizon.net (EHLO antoine-levy-lamberts-macbook.local) [74.101.86.127] by mail.gmx.net (mp045) with SMTP; 12 Jan 2011 21:22:58 +0100 X-Authenticated: #22961642 X-Provags-ID: V01U2FsdGVkX1+w3vEgacOwso7WlU4F181hVTTdBmOySV9u9/9+27 N0eA1SnKXTcVhh Message-ID: <4D2E0DA0.6050105@gmx.de> Date: Wed, 12 Jan 2011 15:22:56 -0500 From: Antoine Levy Lambert User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101207 Thunderbird/3.1.7 MIME-Version: 1.0 To: Ant Users List Subject: Re: launch Ant from Java code References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 Hello Patrick, you can have a look at this document from our manual too : http://ant.apache.org/manual/antexternal.html Creating a Project object is necessary in nearly all cases because ant tasks generally need to be bound to a project in order to be able to log. I have another example which is similar to the one above but is more complete : package com.acme.anttasks; import org.apache.tools.ant.DefaultLogger; import org.apache.tools.ant.Project; import org.apache.tools.ant.Target; /** */ public class SomeTaskWrapper { public static void main(String[] args) { Project antProject = new Project(); SomeTask someTask = new SomeTask(); // create a target, since every execute in Ant need a target Target antTarget = new Target(); antTarget.setName("targetName"); antProject.addTarget(antTarget); // specify that this target will execute SomeTask antTarget.addTask(someTask); DefaultLogger antDefaultLogger = new DefaultLogger(); antDefaultLogger.setEmacsMode(true); antDefaultLogger.setErrorPrintStream(System.err); antDefaultLogger.setOutputPrintStream(System.out); antDefaultLogger.setMessageOutputLevel(Project.MSG_INFO); antProject.addBuildListener(antDefaultLogger); antProject.executeTarget("targetName"); } } Regards, Antoine On 1/12/11 8:08 AM, Patrick Martin wrote: > Hello and happy new year to you all, > > What is the recommended way for launching an Ant script/task from Java code ? > > It seems to me that the following URL > http://ant.apache.org/manual/running.html#viajava > rather explains how to launch Ant with java.exe from the command line. > Is it also recommended to launch the main() method from with a Java > code? Or is there a launch API that can be used? > > I also found people working directly with the Ant project class. Something like: > Project p = new Project(); > p.initProperties(); > p.setBaseDir(getBaseDir(baseDir, buildFile)); > p.setUserProperty("ant.file", getBuildFile(buildFile)); > try { > p.fireBuildStarted(); > p.init(); > ProjectHelper helper = ProjectHelper.getProjectHelper(); > p.addReference("ant.projectHelper", helper); > helper.parse(p, buildFile); > p.executeTarget(null == target ? p.getDefaultTarget() : target); > p.fireBuildFinished(null); > } catch (BuildException e) { > p.fireBuildFinished(e); > [...] > } finally { > [...] > } > > Thank you, > > Patrick > > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@ant.apache.org For additional commands, e-mail: user-help@ant.apache.org