43

How to set the path environment variable from ant script

coelhudo
  • 4,710
  • 7
  • 38
  • 57
user855
  • 19,048
  • 38
  • 98
  • 162

7 Answers7

67

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.

David W.
  • 105,218
  • 39
  • 216
  • 337
  • The first solution worked for me. I'm running RHEL 5.4 and had to set my PYTHONPATH in an ant exec task that needed it. – Alessandro Feb 14 '13 at 13:44
  • 3
    On my machine (Win7), it had to be env.Path, not env.PATH. – Nate Glenn Apr 19 '13 at 19:37
  • 4
    @NateGlenn In Unix and in Java (so thus in Ant too), environment variable names are case sensitive. `PATH` is different from `Path` and from `path`. In Windows, environment variable names aren't case sensitive. Thus, `Path`, `path`, and `PATH` are all the same, but when you use Ant, these represent three different variables. In Unix, it will always be `PATH`. In Windows, I'm not even sure if its the same from machine to machine. You can use `` task to see what your system does with environment variables. – David W. Apr 21 '13 at 22:36
  • @DavidW. Hey, thanks! I was looking for a command that would do that. – Nate Glenn Apr 23 '13 at 00:39
  • what about for a task? [the java task docs](https://ant.apache.org/manual/Tasks/java.html) don't say anything. This needs to work on cross platform. I'm trying to set a variable called GAZEBO_MASTER_URI – Peter Mitrano Jul 27 '15 at 15:10
  • Note that for Windows, you need to use a semicolon (;) instead of a double-colon (:) when concatenating the paths -- it's an easy thing to miss. – MicroVirus Sep 21 '18 at 09:18
5

In ant, properties are immutable, so David's suggestion above:

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

won't work.

But (with the antcontrib-library) variables are mutable, so this works:

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

NOTE: to use the antcontrib-library download it from here: ANT Contrib - Download

This gets the job done, but seems like a dastardly trick.

So to your specific question, try:

<taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <pathelement location="${basedir}/lib/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

<var name="env.PATH" value="some:custom:value"/>
Jerry
  • 669
  • 8
  • 17
  • 3
    This is a neat answer, but note that the variable task isn't part of the standard Ant distribution; you have to install ant-contrib. – gareth_bowles Aug 14 '13 at 16:45
  • 1
    Also, this won't actually update the windows environment variable just the ant property env.PATH – Clintm Jun 03 '15 at 17:57
2

You can use setx command to set the environment variables.

For setx command help refer http://ss64.com/nt/setx.html

<exec executable="setx.exe">
  <arg line="Path C:\jdk1.5.0_12\bin"/>
  <arg line="/m"/>
</exec>
Rutwig
  • 37
  • 1
  • 1
    From the setx documentation: "2) On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window." So this won't work as a solution for the question, unless the user wants to open a new shell. – Jerry Apr 24 '13 at 14:59
1

I found it works by quote the value of variable

<exec executable="setx">
    <arg line="Path &quot;${env.Path};c:\testPath&quot;" />
    <arg line="/m" />
</exec>
komelio
  • 19
  • 1
1

You can use to expand the path:

And then you can execute for instance sh from ant to export the environment variable:

<property environment="env"/>
<exec executable="sh">
   <arg value="-c"/>
   <arg value="export PATH=${env.Path}:${myPath}"/>
</exec>

Or execute your command and set env with value, like so:

<property environment="env"/>
<exec executable="${your.command}">
   <env key="PATH" value="${env.PATH}:${your.directory}"/>
</exec>
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
0

since I don't have enough reputation to comment on the <variable ... suggestions my comment as an answer ... :-/

In ("newer") ant-contrib (extra ANT package) the task is not called <variable ... but <var ...!

(but it didn't work for me anyways since I think the manipulation of the env.* (created by <property environment="env" ... /> task) Java properties/variables is only relevant for tasks/processes evaluating these Java properties which are not automatically "synced back" to the OS environment variables)

Andreas Covidiot
  • 4,286
  • 5
  • 51
  • 96
0

To set the environment variables through Ant, try calling exec task and set the command line values. I have not tried this by the way, but it should work.

Vicky
  • 121
  • 1
  • 1
  • 6