This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Ant, Maven and Other Build Tools and the fly likes How to set environmental variables from ANT? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "How to set environmental variables from ANT?" Watch "How to set environmental variables from ANT?" New topic

How to set environmental variables from ANT?

Micky Pat

Joined: May 03, 2006
Posts: 18
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}"/>

if I run this, the output will be

[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?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32481

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Have you checked out Aspose?
subject: How to set environmental variables from ANT?
It's not a secret anymore!