Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set environmental variables from ANT?

 
Micky Pat
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I know this is not clean, but I would need to get this to work (don't ask me why... )

Hi need to set 2 environmental variables, MYTESTVARIABLE1 and MYTESTVARIABLE2, set from ant.
Note that I preset MYTESTVARIABLE1=ggg1 and MYTESTVARIABLE2=ggg2.
Now, if I run the following ant script

<project basedir="." default="test" name="Setting env variable">

<property environment="env"/>
<property name="env.MYTESTVARIABLE1" value="ttt1"/>
<property name="env.MYTESTVARIABLE2" value="ttt2"/>

<target name="test">
<echo message = "Variable 1 is = ${env.MYTESTVARIABLE1}"/>
<echo message = "Variable 2 is = ${env.MYTESTVARIABLE2}"/>
<echo message = "ANT_HOME is set to = ${env.ANT_HOME}"/>
<echo message="Number of Processors = ${env.NUMBER_OF_PROCESSORS}"/>
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>
</target>
</project>

if I run this, the output will be

test:
[echo] Variable 1 is = ggg1
[echo] Variable 2 is = ggg2
[echo] ANT_HOME is set to = ${env.ANT_HOME}
[echo] Number of Processors = ${env.NUMBER_OF_PROCESSORS}
[echo] ANT_HOME is set to = ${env.ANT_HOME}


which is not what I am expecting...

What's wrong? Is there a way of setting environmental variables?

Regards
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Micky,
The property task can only be used for getting the value of an already set environment variable. There is no way to set environment variables through Ant. You would need to call Ant's exec task and pass the command line (for your operating system) to set the value of an environment variable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic