permaculture playing cards*
The moose likes Ant, Maven and Other Build Tools and the fly likes Setting an environment variable in Linux Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Setting an environment variable in Linux" Watch "Setting an environment variable in Linux" New topic
Author

Setting an environment variable in Linux

Vijaykumar Patil
Greenhorn

Joined: Jan 15, 2013
Posts: 6
Hello,

I want to set an environment variable in Linux while building using ant.

For example:

If I run the following command,

$ ant test

The "test" target has to set the environment variable foo=bar.
Means the "test" target has to run the command `export foo=bar`.

So far investigation I know I have to use on wrapper scripts which does it.
But it is not working either for me.

Please help!


Thanks,
Vijay
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

I tried my hands on, but I am afraid that we can not set environment variable using ant.
Vijaykumar Patil
Greenhorn

Joined: Jan 15, 2013
Posts: 6
Hi Harshavardhan,

Thanks for the reply and trying it.

After googling, I came to know that we can set the environment variable using a wrapper shell script.
But I need an working example if anybody has here. As I also tried, but no luck.


Thanks,
Vijay
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2274
    
  28

What have you tried already?
Vijaykumar Patil
Greenhorn

Joined: Jan 15, 2013
Posts: 6
This is what I tried,

mybuild.xml:

<project name="Sample">
<target name="test">
<exec executable="/bin/bash">
<arg line="./sample.sh bar"/>
<!--<arg value="bar"/>-->
</exec>
</target>
</project>



sample.sh:

#!/bin/bash
export foo=bar


$ ant -f mybuild.xml test

test:

BUILD SUCCESSFUL
Total time: 0 seconds

$ echo $foo


$

But the environment variable foo didn't set
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

It did get set, but only in the shell launched by Ant. Once that shell exited, the value disappeared. I think that even is you ran "sh sample.sh" that foo would not be set in your shell, you would have to do something like ". sample.sh" which causes the script to be run in your current shell.

But rather that trying to do something that is impossible, why don't you tell us why your real requirements are, we might be able to provide some suggestions. Being able to set an env var via Ant is a proposed technical solution to a problem, it is not a requirement. Tell us what the problem is.


JBoss In Action
Vijaykumar Patil
Greenhorn

Joined: Jan 15, 2013
Posts: 6
Hi Peter,

Thanks for replying.

I agree with you that the env var foo will not set in the current shell.
But how to set in the current shell? is there any way to do it by ant?
Yes, ". sample.sh" or "source sample.sh" will set the env var foo to bar.
How to do it using ant (build.xml) ?

The requirement is:
If ant runs the target new_target, should set the env var foo to bar.
$ ant new_target
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

You cannot do it using Ant, it is not possible.
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Can you do something like



And read it in your build.xml like this


Thanks and Regards,
~Sujoy
Vijaykumar Patil
Greenhorn

Joined: Jan 15, 2013
Posts: 6
Thank you for the reply Peter.
Vijaykumar Patil
Greenhorn

Joined: Jan 15, 2013
Posts: 6
Hi Sujay,

Thank you for the reply.

I want to set the env variable only through the target but not with command line argument.



Thanks,
Vijay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting an environment variable in Linux
 
Similar Threads
Running .cmd file using "ant"
How do I provide a default for an environment variable accessed via ${env.VAR_NAME} in build script?
Chap.10 Development , Q5 pg813 : not understood answer and cannot run code
NoClassDefFoundError
ant and environment classpath