从java程序运行build.xml

时间:2011-07-12 11:48:40

标签: java ant

我正在尝试从java程序运行ant脚本。执行程序的过程是什么 如何从java程序运行build.xml?

这是我试图实施的方式

Process proc = rt.exec("ant -buildfile D:ant\\trail");

的问候, 技术人员

3 个答案:

答案 0 :(得分:5)

点击Execute Ant From Your Application  看看这个例子:

      Project project = new Project();
      project.init();
      DefaultLogger logger = new DefaultLogger();
      logger.setMessageOutputLevel(Project.MSG_INFO);
      logger.setErrorPrintStream(System.err);
      logger.setOutputPrintStream(System.out);
      project.addBuildListener(logger);

      File buildFile = new File("buildhtml.xml");
      ProjectHelper.configureProject(project, buildFile);
      project.setProperty("ant.file", buildFile.getAbsolutePath());
      project.setProperty("item", "ant");
      project.setProperty("inputdir", "src/items/ant");
      project.setProperty("outputdir", "build/items/ant");
      project.setProperty("graphics.prefix", "../../");
      try {
         project.executeTarget("checkifuptodate");
      } catch(Exception e) {System.err.println(e.getMessage());}
      // rest of program goes here

这比调用Runtime.exec

更好

答案 1 :(得分:1)

不是单独尝试启动Windows可执行文件,而是使用Ant API的更强大,更灵活的解决方案。文档包含在ant本身中,它们是not online ...

答案 2 :(得分:-1)

试试Runtime.getRuntime().exec("cmd /c start ant.bat"); 取自How do I run a batch file from my Java Application?