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

时间:2009-11-02 19:21:17

标签: java ant

任何想法,为什么在通过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>

有什么想法吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

如果添加标记的输出属性,问题将解决。

答案 1 :(得分:0)

使用java代码执行任何任务的Ant构建脚本示例:

     
  • 将日志指向控制台和字节数组;
  •  
  • 填写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;

}