I have tried invoking Ant programmatically, but I am encountering this error
Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/AntMain
I tried to run build.xml separately using the console and via right-clicking it in eclipse running as ant build. It runs with no problems.
My sample ant testing class
public class AntTest {
public static void main(String[] args) {
File buildFile = new File("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
}
}
My sample build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="testproject" default="test" basedir=".">
<target name="test">
<echo message="Hello World" />
</target>
</project>
What am I missing?
Make sure that the ant libraries are in your classpath.
It seems, that
ant-launcher-VERSION.jar(where Version is the ant version you are using) is missing in your classpath.