2011-07-12 107 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 ...