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

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}