How to set ANT_HOME on Ubuntu Desktop 12.04?

﹥>﹥吖頭↗ 提交于 2021-02-06 11:38:09

问题


It looks like Ubuntu uses ~/.bashrc, ~/.bash_profile, ~/.pam_profile, /etc/environment, and /etc/profile in very similar ways. I'd like to be able to add a configuration to one of these (which ever is the appropriate one) to set ANT_HOME to be the absolute path to my Ant installation's root directory (happens to be /opt/apache/ant/1.8.4/apache-ant-1.8.4/). This variable needs to be "honored" as is any normal env var, where I can open up a terminal and echo it at any time. It would also be nice if I could set this in such a way for Java to read it in at runtime from a System.getProperty("") call.

  • Which file do I use?
  • How do I actually set it so that it meets my requirements above?

Thanks in advance for any help or pointers here!


回答1:


For global settings, system-wide environment variables

  • Use /etc/environment
  • don't use /etc/profile, or /etc/bash.bashrc

From this page :

/etc/environment [...] is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

Using /etc/profile is a very Unix-y way to go, but its functionality is greatly reduced under Ubuntu. It exists only to point to /etc/bash.bashrc and to collect entries from /etc/profile.d .

On my system, the only interesting entry entry in profile.d is /etc/profile.d/bash_completion.sh .

For local or per-user settings

A previous version of the Ubuntu page recommended ~/.pam_environment , but the page currently suggests that if that doesn't work, you should use

  • ~/.profile - This is probably the best file for placing environment variable assignments in, since it gets executed automatically by the DisplayManager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.

  • ~/.bash_profile or ~./bash_login - If one of these exists, bash executes it instead of "~/.profile" when bash is started as a login shell. Bash will prefer ~/.bash_profile to ~/.bash_login. [...] These files won't influence a graphical session by default."

  • ~/.bashrc - "... may be the easiest place to set variables".




回答2:


First, open bash file with following code.

xxx@xxx-desktop:~$ sudo gedit /etc/bash.bashrc

Then, Insert Java home as path to JDK location and ANT home as path ANT location as mentioned below at the end of bash file. I entered path for locations according to my machine.

  export ANT_HOME=/usr/share/ant
 export JAVA_HOME=/usr/lib/jvm/java-6-sun
 set path=$path $ANT_HOME/bin

Eventually, save and close the file. If you configured correctly, the terminal must show following note with the command “ant –version”.

      xxx@xxx-desktop:~$ ant -version

Apache Ant version 1.7.1 compiled on November 10 2008 It means configuration is Ok.




回答3:


Firstly, it's standard practice to omit the trailing slash when setting env variables on *nix. (You have a trailing slash at the end of yours). So you should write:

export ANT_HOME=/opt/apache/ant/1.8.4/apache-ant-1.8.4

... and not:

export ANT_HOME=/opt/apache/ant/1.8.4/apache-ant-1.8.4/

~/.bashrc, ~/.bash_profile are good if you only care about your user account and you use the bash shell. For setting an environment variable, it doesn't really make much difference which of these you use. .bashrc would reset it every time you opened a new shell, whereas .bash_profile would reset it every time you logged-in.

The files in /etc would set it for all users on your system (but it could be overridden locally). If you're going to have different users building and you want them all to have the same environment varilables, then /etc/profile would be a good place to put it.




回答4:


To read the variable from java, use System.getenv().get("ANT_HOME"). Read more about it here.

As for where to set it, I prefer using ~/.bashrc, unless you're going to run your program from other users, or with sudo. Then you should use /etc/environment




回答5:


I'd prerer to put all environment variables to different file and source it from $HOME/.xsessionrc. .xsessionrc is simply a shell script which is executed after the new X session by login manager like gdm, kdm or so.




回答6:


BEST way to set environment variables GLOBALY

Step 1 :
Set all variables in /etc/environment like this

JAVA_HOME=/usr/lib/jvm/java-6-sun
ANT_HOME=/usr/....<path to ant home>
set path="/usr/bin:<path2>:$JAVA_HOME/bin:$ANT_HOME/bin"

Step 2 :
Add this line at the end of ~/.bashrc of each user

source /etc/environment

Step 3 :
Execute the following command to make the changes.

source ~/.bashrc



Hope it helps..!!



来源:https://stackoverflow.com/questions/12224804/how-to-set-ant-home-on-ubuntu-desktop-12-04

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!