Reputation: 711
Any idea, why none of the debugging comments are printed once after executing the ANT build script's SQL task via java code?
The java class to execute the sql in build scirpt is
public class AntRunnerTest {
private Project project;
public void executeTask(String taskName) {
try {
project = new Project();
project.init();
project.setBasedir(new String("."));
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, new File("build-copy.xml"));
System.out.println("Before");
project.executeTarget(taskName);
System.out.println("After");
} catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String args[]) {
try {
AntRunnerTest newInst = new AntRunnerTest();
newInst.executeTask("sql");
} catch(Exception e) {
System.out.println(""+e);
}
}
}
I dont see the debug String "After" getting printed in the console. I noticed this issue only when i try to execute a sql task using java code.
The ant script has the following simple transaction tag in it.
<transaction> <![CDATA[ select now() ]]> </transaction>
Any thoughts?
Thanks in advance.
Upvotes: 1
Views: 1343
Reputation: 21
Example of an Ant build script executing any task using java code:
See source code: https://github.com/wellboyvg/workgear/blob/master/manager/src/manager/WorkGearManager.java
private boolean executeAntTask(String target) {
boolean success = false;
// use log output to the console
DefaultLogger conlog = new DefaultLogger();
conlog.setErrorPrintStream(System.err);
conlog.setOutputPrintStream(System.out);
conlog.setMessageOutputLevel(Project.MSG_INFO);
// use log output to the byte array
DefaultLogger strlog = new DefaultLogger();
ByteArrayOutputStream errb = new ByteArrayOutputStream();
PrintStream errp = new PrintStream(errb);
strlog.setErrorPrintStream(errp);
ByteArrayOutputStream outb = new ByteArrayOutputStream();
PrintStream outp = new PrintStream(outb);
strlog.setOutputPrintStream(outp);
strlog.setMessageOutputLevel(Project.MSG_INFO);
// prepare Ant
Project project = new Project();
File buildfile = new File(buildname);
project.setUserProperty("ant.file", buildfile.getAbsolutePath());
// add record log to the console
project.addBuildListener(conlog);
// add record log to the byte array
project.addBuildListener(strlog);
//
try {
// fill the Ant project variables
for (Entry m : map.entrySet()) {
project.setUserProperty(m.getKey().toString(), m.getValue().toString());
}
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildfile);
// execute the ant task
project.executeTarget(target);
project.fireBuildFinished(null);
success = true;
} catch (BuildException buildException) {
project.fireBuildFinished(buildException);
}
// add output log to swing component (javax.swing.JTextArea)
jtLog.append(new String(outb.toByteArray()));
jtLog.append(new String(errb.toByteArray()));
return success;
}
Upvotes: 0