使用java代码执行< sql>任务的ant构建脚本

yrwegjxp  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(371)

你知道为什么在通过java代码执行ant构建脚本的sql任务之后,没有一个调试注解被打印出来吗?
在build scirpt中执行sql的java类是

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);
      }
   }
}

我看不到控制台中打印的调试字符串“after”。只有在尝试使用java代码执行sql任务时,我才注意到这个问题。
ant脚本中有以下简单的事务标记。

<transaction> <![CDATA[ select now() ]]> </transaction>

有什么想法吗?
提前谢谢。

fnx2tebb

fnx2tebb1#

使用java代码执行任何任务的ant构建脚本示例:
将日志定向到控制台和字节数组;
填写ant项目变量;
执行蚂蚁任务;
将输出日志添加到swing组件(javax.swing.jtextarea);
请参阅源代码: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;

}

a0x5cqrl

a0x5cqrl2#

如果添加tag的output属性,问题就迎刃而解了。

相关问题