ant脚本和java程序间的互相调用

1,ant脚本调用java程序
  a,建立一个java类,继承ant的Task类:
  package org.example;

  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Task;
  
  public class ExampleTask extends Task {
   private String msg;
  
   // The method executing the task
   public void execute() throws BuildException {
    System.out.println(msg);
   }
   // The setter for the "message" attribute
   public void setMessage(String msg) {
    this.msg = msg;
   }
  }
 b,ant脚本:
     <?xml version="1.0"?>

   <project name="ExampleTask" default="main" basedir=".">
    <taskdef name="mytask" classname="org.example.ExampleTask">
     <classpath>
      <pathelement location="./classes" />
     </classpath>
    </taskdef>
    <target name="main">
     <mytask message="this is a example." />
    </target>
   </project>
  c,运行ant脚本。
 
2,java程序调用ant脚本
  1,建立一个java类,
  package org.example;
 import java.io.File;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.DefaultLogger;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.ProjectHelper;
 
 public class AntExample {
 
  /**
   * @param args
   */
  public static void main(String[] args) {
   File buildFile = new File("example.xml");
   Project p = new Project();
   try {
    p.fireBuildStarted();
    p.init();
    ProjectHelper helper = ProjectHelper.getProjectHelper();
    helper.parse(p, buildFile);
    p.executeTarget(p.getDefaultTarget());
    p.fireBuildFinished(null);
   } catch (BuildException e) {
    p.fireBuildFinished(e);
   }
  }
 }
 
 b,ant脚本:
  <?xml version="1.0"?>
 <project name="ant4J" default="main" basedir=".">
  <target name="main">
  <echo message="ant file for java " />
 </target>
 </project>
 c,运行java程序。

posted @ 2009-08-06 09:14  121543988  阅读(2127)  评论(0编辑  收藏  举报