7

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%**
user1587504
  • 764
  • 1
  • 10
  • 25

2 Answers2

5

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
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
  • Accepted your answer. Small query, for accessing %MY_VAR% outside of exec, I need to again use property ant task?. – user1587504 Nov 11 '13 at 16:27
  • copying into new property won't work! :( exec doesn't support the nested "property" element. – user1587504 Nov 11 '13 at 16:39
  • 1
    As I've posted it, MY_VAR only exists for the lifetime of the `` task. If you need some kind of property to reference after the task completes, set a property before the call to `` and pass that reference to ``: ` ...` – Chad Nouis Nov 11 '13 at 16:43
0

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

<property environment="env"/>
<property name="MY_VAR" value="${env.MY_VAR}"/>
  • We cannot use property inside exec. 'exec doesn't support the nested "property" element.' . I am trying to set some variable with 'set MY_VAR=SOME_VAL' on windows & similar way want to use export on *nix – user1587504 Nov 11 '13 at 09:55