user1587504
user1587504

Reputation: 764

setting env variable with ant's exec task - doesn't seem to work

When I try to set some varible with ant's exec task, it doesn't seem to set to my required value. Not sure what's wrong here.

It works perfectly file when I set & echo from command line with cmd.

<exec executable="cmd">
    <arg value="set"/>
    <arg value="MY_VAR=SOME_VAL"/>
</exec>
-->
<echo message="MY_VAR is set to %MY_VAR%"/>

And output looks like:

exec
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\MY_PROJ_BASE_DIR_HERE>
echo
MY_VAR is set to **%MY_VAR%**

Upvotes: 7

Views: 9487

Answers (2)

Chad Nouis
Chad Nouis

Reputation: 7041

Use the /C option of cmd.exe.

build.xml

<project name="ant-exec-cmd-with-env-key" default="run">
    <target name="run">
        <exec executable="cmd" failonerror="true">
            <env key="MY_VAR" value="SOME_VAL"/>
            <arg value="/c"/>
            <arg value="echo %MY_VAR%"/>
        </exec>
    </target>
</project>

Output

run:
     [exec] SOME_VAL

Upvotes: 5

Sietse van der Molen
Sietse van der Molen

Reputation: 705

Are you sure the problem is not in your reading of the variable?

<property environment="env"/>
<property name="MY_VAR" value="${env.MY_VAR}"/>

Upvotes: 0

Related Questions