2012-07-04 46 views
1

我試圖通過this answer在ant build.xml上設置PATH環境變量。在ant build.xml上設置PATH環境變量適用於cygwin,但不適用於cmd或PowerShell

它適用於cygwin,但不適用於cmd或PowerShell。

一些信息:

Apache Ant的1.6.5(我知道有一個新的版本(1.8.4),但對於內部的原因,我不得不使用此舊版本)

PowerShell的2.0版

CMD v6.1.7601

cygwin的2.774

Windows 7的

+0

你想使用'exec'任務嗎? – pb2q

+0

是的,就像這個問題一樣http://stackoverflow.com/q/5607580/174605 – coelhudo

回答

0

不幸的是與1.6.5版本有關的一個螞蟻bug。我能夠更新到1.8.4,一切正常。

3

您可能需要在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> 
相關問題