This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting an environment variable in Linux

 
Vijaykumar Patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried my hands on, but I am afraid that we can not set environment variable using ant.
 
Vijaykumar Patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried already?
 
Vijaykumar Patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijaykumar Patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot do it using Ant, it is not possible.
 
Sujoy Choudhury
Ranch Hand
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you do something like



And read it in your build.xml like this
 
Vijaykumar Patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply Peter.
 
Vijaykumar Patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic