coelhudo
coelhudo

Reputation: 5080

Setting PATH environment variable on ant build.xml works on cygwin but not on cmd or PowerShell

I was trying to set PATH enviroment variable on an ant build.xml through this answer.

It works on cygwin but not on cmd or PowerShell.

Some information:

Apache Ant 1.6.5 (I know there is a newer version (1.8.4), but for internal reasons I have to use this older version)

Powershell v2.0

cmd v6.1.7601

cygwin 2.774

Windows 7

Upvotes: 1

Views: 5096

Answers (2)

coelhudo
coelhudo

Reputation: 5080

Unfortunatelly was an ant bug related to 1.6.5 version. I was able to update to 1.8.4 and everything works fine.

Upvotes: 0

pb2q
pb2q

Reputation: 59617

You may need to use the exec task a little differently on Windows/cmd environments.

Let's use the windows command set as an example. set will print environment variables. A normal exec task running a set command might look like:

<exec executable="set" outputproperty="set.output">
    <env key="MY_VAR" value="MY_VAL"/>
    <echo message="${set.output}"/>
</exec>

But using this form of the exec task should throw an IOException: The system cannont find the file specified.

When running ant under windows cmd shell, the exec task can also be invoked via cmd, like this:

<exec executable="cmd" outputproperty="set.output">
    <arg line="/c set"/>
    <env key="MY_VAR" value="MY_VAL"/>
    <echo message="${set.output}"/>
</exec>

This is the equivalent command; the actual command executed is cmd /c set, which is running set in a cmd sub-process.

The reason why this is necessary is only a little complicated and is due to the way that the commands are located by Win32 ::CreateProcess. The ant exec docs briefly explain this.

Note that I haven't tried either of these using PowerShell, so I have no experience which, if either, will work.

In my own ant build scripts I typically have two versions of each target that require special handling for windows platforms, with an isWindows test that looks like this:

<target name="check-windows">
    <condition property="isWindows">
        <os family="windows"/>
    </condition>
</target>

Then I can switch between versions of the same task using:

<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>

Upvotes: 3

Related Questions