How to set the path environment variable from ant

2019-01-14 00:37发布

How to set the path environment variable from ant script

7条回答
姐就是有狂的资本
2楼-- · 2019-01-14 00:48

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)

查看更多
小情绪 Triste *
3楼-- · 2019-01-14 00:50

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>
查看更多
Ridiculous、
4楼-- · 2019-01-14 00:54

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.

查看更多
Deceive 欺骗
5楼-- · 2019-01-14 00:54

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>
查看更多
成全新的幸福
6楼-- · 2019-01-14 01:00

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.

查看更多
Fickle 薄情
7楼-- · 2019-01-14 01:06

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>
查看更多
登录 后发表回答