Jay
Jay

Reputation: 711

Ant build script executing <sql> task using java code

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

Answers (2)

Viktor Wellboy
Viktor Wellboy

Reputation: 21

Example of an Ant build script executing any task using java code:

  • direct the log to the console and byte array;
  • fill in the Ant project variables;
  • execute an ant task;
  • add output log to swing component (javax.swing.JTextArea);

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

Jay
Jay

Reputation: 711

The problem will be solved if you add the output attribute of tag.

Upvotes: 1

Related Questions