您可能需要在Windows/cmd環境中使用略有不同的exec
任務。
讓我們以windows命令set
爲例。 set
將打印環境變量。
<exec executable="set" outputproperty="set.output">
<env key="MY_VAR" value="MY_VAL"/>
<echo message="${set.output}"/>
</exec>
但是,使用這種形式的exec
任務的應該拋出IOException:系統cannont找到指定的文件正常exec
任務運行set
命令像看起來。
當窗戶在cmd殼下運行ant,所述exec
任務也可以通過調用cmd
,像這樣:
<exec executable="cmd" outputproperty="set.output">
<arg line="/c set"/>
<env key="MY_VAR" value="MY_VAL"/>
<echo message="${set.output}"/>
</exec>
這是等效的命令;執行的實際命令是cmd /c set
,它在cmd子進程中運行set
。
這是必要的原因只是有點複雜,並且是由於Win32 ::CreateProcess
命令的位置。 ant exec docs簡要解釋這一點。
注意,我還沒有嘗試使用PowerShell的這些任何一個,所以我沒有經驗,如果其中任何一個,將工作。
在我自己的Ant構建腳本我通常每一個目標的兩個版本,需要對Windows平臺的特殊處理,具有isWindows
測試,看起來像這樣:
<target name="check-windows">
<condition property="isWindows">
<os family="windows"/>
</condition>
</target>
然後,我可以的版本之間切換同樣的任務使用:
<target name="my-target-notwindows" depends="check-windows" unless="isWindows>
...
</target>
<target name="my-target-windows" depends="check-windows" if="isWindows>
...
</target>
<target name="my-target" depends="my-target-notwindows,my-target-windows">
...
</target>
你想使用'exec'任務嗎? – pb2q
是的,就像這個問題一樣http://stackoverflow.com/q/5607580/174605 – coelhudo