How to call an Ant script (part 2 - with parameters)
Join the DZone community and get the full member experience.
Join For FreeThe 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));
}
myLogger.setErrorPrintStream(System.err);
myLogger.setOutputPrintStream(System.out);
myLogger.setMessageOutputLevel(Project.MSG_INFO);
project.addBuildListener(myLogger);
try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
project.fireBuildFinished(null);
} catch (Exception e) {
project.fireBuildFinished(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
application
Classpath (Java)
Pass (software)
Library
Opinions expressed by DZone contributors are their own.
Comments