• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting an environment variable in Linux

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried already?
 
Vijaykumar Patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot do it using Ant, it is not possible.
 
Ranch Hand
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply Peter.
 
Vijaykumar Patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic