Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to call an Ant script (part 1 - without parameters)

DZone's Guide to

How to call an Ant script (part 1 - without 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 without parameters. All you need to pass through the name and path of the Ant script:


private boolean runAntScript(String scriptName) {

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

project.setUserProperty("ant.file", buildFile.getAbsolutePath());
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-1-without.html

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

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}