|
Executing Ant code pragramatically
Hello,
I am trying to execute an ant script programmatic ally via Java code. I know the build file works, because when I run it in Eclipse, it does exactly what I want it to. However, when I try to run the Java class that executes the build script, I have a problem. The script starts to run, but fails on the <junit> task because it can't find it. Within Eclipse, it works since I have the classpath parameters set up, so it knows where to look for the jar file.
What I need to know how to do is either:
a) how to modify the library that ant uses with Java code (ie: basically doing "ant -lib junit.jar" in java code OR
b) if there is an easier way to do this altogether (such as using AntRunner, or executing command line commands within java code)
I have done several google searches, and the way I have started now is the only way that I have found so far. Here is the code snippet that launches the and build file:
Project project = new Project();
project.setUserProperty("ant.file", buildFile.getAbsolutePath());
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_IN FO);
project.addBuildListener(consoleLogger);
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 (BuildException e) {
project.fireBuildFinished(e);
}
Thank you in advance for any help. It is greatly appreciated.
Edit: I should also mention that the classes being used are in the org.apache.tools.ant package.
Last edited by MikeO : 07-24-2007 at 11:44 PM.
|