2014-01-08 197 views
0

我有一個這樣的命令:如何將ant命令行參數傳遞給ant java項目?

Ant命令:

<path-to-ant/bin/ant -f build.xml sql -lib <path-to-lib>/lib -Dhome=path-home -Ddev=path-to-dev -DsdkMode=install -DcompileSource.dest=path-to-dest -DOSTEMP=/tmp -D-BIFLocation=path-to-loation -DexecutionDirectory=path-exec-dir 

在beggining,我們使用ProcessBuilder builder = new ProcessBuilder(command);運行這個,currentlyric我們希望與ANT API運行它,所以我們用Ant項目構建它。 我只是建立等的方法:

// string value after "-f" is the location of buildFile 
    File buildFile = new File(antCommand.get((antCommand.indexOf("-f")+ 1))); 

    // string value before "-lib" is the target to run 
    String traget = antCommand.get((antCommand.indexOf("-lib")- 1)); 

    Project project = new Project(); 
    project.setUserProperty("ant.file", buildFile.getAbsolutePath()); 
    project.init(); 

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

    ProjectHelper projectHelper = ProjectHelper.getProjectHelper(); 
    project.addReference("ant.projectHelper", projectHelper); 
    projectHelper.parse(project, buildFile); 

    project.executeTarget(traget); 

,但我不知道如何將參數從使用Java -D啓動命令行中傳遞。

任何人有想法或這種經驗? 非常感謝。

回答

0

其添加爲在build.xml目標

<sysproperty key="sdkMode" value="sdkMode" /> 

系統屬性......然後抓住它的代碼

String sdkMode = System.getProperty("sdkMode"); 

...當然其指定的命令line

ant <run-target> -DsdkMode=TEST 
+0

所以所有以-D開頭的參數只是一個系統屬性,當它在ant命令行中執行時,明白了,謝謝。所以我可以使用: System.setProperty(key,value);設置它? –

相關問題