2011-07-12 112 views
2

我想从java程序运行ant脚本。什么是程序执行程序 如何从java程序运行build.xml?从java程序运行build.xml

这里是如何IAM试图执行

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

问候, 技术人员

+0

win/* nix系统? –

+0

windows和eclipse – techie

+0

如果你有eclipse,那么为什么从java调用它? –

回答

-1

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

+0

但我们必须给build.xml文件?然后从我们必须提供 – techie

+0

您尝试过吗?Runtime.getRuntime()。exec(“cmd/c start ant.bat -buildfile D:ant \\ trail”);'但是当你描述你的问题,我会建议简单runAnt.bat –

+0

这就是我得到的Buildfile:D:ant \ trail不存在! Build failed – techie

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

而不是试图单独启动一个Windows可执行一个更好的解决方案,这将会是使用Ant的API一个更强大和灵活的解决方案。文件包含在蚂蚁本身中,它们是not online ...