How to set the path environment variable from ant script

user855 picture user855 · Apr 9, 2011 · Viewed 76.3k times · Source

How to set the path environment variable from ant script

Answer

David W. picture David W. · Apr 11, 2011

Is this for an <exec> task?

You can set environment variables when you run an <exec> task:

<exec executable="${my.command}">
    <env key="foo" value="bar"/>
    <arg line="some value"/>
</exec>

You can use <property environment="env"/> to expand the path:

<property environment="env"/>
<exec executable="${my.command}">
   <env key="PATH" value="${env.PATH}:${my.directory}"/>
</exec>

If this is for some custom task that requires an environment variable, but doesn't allow you to set the environment variable in the task if one isn't set, you can try setting it in:

<property environment="env"/>
<property name="env.foo" value="bar!bar"/>

This might set an environment variable called foo to the value of bar!bar!. I remember something about this, but wasn't able to get it to work.

The other thing you can do is have one ant script execute another and have the first ant script set the environment value. I did this when I had to set ANT_OPT.