You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Patrick Martin <an...@gmail.com> on 2011/01/12 14:08:35 UTC

launch Ant from Java code

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


Re: launch Ant from Java code

Posted by Gilbert Rebhan <gi...@maksimo.de>.
-------- Original Message  --------
Subject: launch Ant from Java code
From: Patrick Martin <an...@gmail.com>
To: Ant User <us...@ant.apache.org>
Date: Wed Jan 12 2011 14:08:35 GMT+0100 (CET)

> 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?


Another option is to use ant via groovy, see =
http://groovy.codehaus.org/Ant+Integration+with+Groovy
http://groovy.codehaus.org/Using+Ant+Libraries+with+AntBuilder


Regards, Gilbert


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


Re: launch Ant from Java code

Posted by Antoine Levy-Lambert <an...@gmx.de>.
On 1/13/2011 2:57 AM, Patrick Martin wrote:
> Thank you Antoine,
>
> I do have a follow-up question on this though. Will this be thread
> safe? Is it possible to run two targets in parallel from Java code,
> without any interactions (different projects, property sets and so
> on)?
Ant has a parallel task, and I am running build scripts with some 
parallelism inside them (for instance to run different JUnit suites in 
parallel).
Running different projects in parallel should work. You should make a 
prototype and see how it goes. If you find errors ping the list and/or 
create bug reports.
> I am especially afraid of the system properties which may be shared
> and set/read from parallel ant projects.
System properties are really handles by Java and they are shared in a 
JVM. So if your different projects rely on different system properties 
then in fact you need to fork them.
> I'd also be interested in running ant tasks in parallel on *different*
> versions of Ant (for script migrations purposes), which would probably
> lead to using different class loaders. Is that something feasible?
I have never tried.
Wrapping ant inside an OSGi bundle could address your need. You could 
create a bundle for say ant 1.6.x and another for ant 1.8.x, and have 
these bundles export a project build service.

> Thanks again,
>
> Patrick
>
Regards,

Antoine

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


Re: launch Ant from Java code

Posted by Patrick Martin <an...@gmail.com>.
Thank you Antoine,

I do have a follow-up question on this though. Will this be thread
safe? Is it possible to run two targets in parallel from Java code,
without any interactions (different projects, property sets and so
on)?
I am especially afraid of the system properties which may be shared
and set/read from parallel ant projects.

I'd also be interested in running ant tasks in parallel on *different*
versions of Ant (for script migrations purposes), which would probably
lead to using different class loaders. Is that something feasible?

Thanks again,

Patrick

On Wed, Jan 12, 2011 at 9:22 PM, Antoine Levy Lambert <an...@gmx.de> wrote:
> 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
>
>

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


Re: launch Ant from Java code

Posted by Antoine Levy Lambert <an...@gmx.de>.
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