Over a million developers have joined DZone.

How to call an Ant script (part 2 - with parameters)

DZone's Guide to

How to call an Ant script (part 2 - with parameters)

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

The method below calls an Ant script with parameters. All you need to pass through is the name (and path) of the Ant script and a Map of parameters – the keys are the parameters names as in the Ant script, and the values are the parameters values:

private boolean runAntScript(String scriptName, Map params) {

File buildFile = new File(scriptName);
Project project = new Project();
DefaultLogger myLogger = new DefaultLogger();

project.setUserProperty("ant.file", buildFile.getAbsolutePath());

Set keys = params.keySet();
Iterator iteratorKeys = keys.iterator();
while (iteratorKeys.hasNext()) {
String key = (String) iteratorKeys.next();
project.setProperty(key, params.get(key));



try {
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildFile);
} catch (Exception e) {
return false;

return true;

You will need the ant libraries in the application classpath. Notice that the above example sends the script output to default System.out and runs the default script start target.

From http://e-blog-java.blogspot.com/2011/04/how-to-call-ant-script-part-2-with.html

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}