How to Apache Ant on Mac OS X

In this tutorial, we will show you how to install Apache Ant on Mac OSX.

Tools :

  1. Apache Ant 1.9.4
  2. Mac OSX Yosemite 10.10
Preinstalled Apache Ant?
In older version of Mac, Apache Ant may be already installed by default, check if Apache Ant is installed :


$ ant -v

1. Get Apache Ant

Visit Apache Ant website, and get the .tar.gz file.

install-apache-ant-on-mac-osx

2. Extracts It

Copy the downloaded gz file to your favorite location, extracts it.


$ cp ~/Downloads/apache-ant-1.9.4-bin.tar.gz .

$ cd ~
$ pwd
/Users/mkyong

$ tar vxf apache-ant-1.9.4-bin.tar.gz

x apache-ant-1.9.4/bin/ant
x apache-ant-1.9.4/bin/antRun
x apache-ant-1.9.4/bin/antRun.pl
x apache-ant-1.9.4/bin/complete-ant-cmd.pl
x apache-ant-1.9.4/bin/runant.pl
x apache-ant-1.9.4/bin/runant.py
x apache-ant-1.9.4/
x apache-ant-1.9.4/bin/
......

$ cd ~/apache-ant-1.9.4/bin
$ pwd
/Users/mkyong/apache-ant-1.9.4/bin

$ ant -v
Apache Ant(TM) version 1.9.4 compiled on April 29 2014
Trying the default build file: build.xml
Buildfile: build.xml does not exist!
Build failed

P.S The Apache Ant commands are available at folder $APACHE_ANT_FOLDER/bin.

3. Environment Variable

Set the command ant as the environment variable, so that you can “ant” build your project everywhere.


$ vim ~/.bash_profile

Exports $ANT_HOME/bin, save and restart terminal.

~/.bash_profile

export JAVA_HOME=$(/usr/libexec/java_home)
export GRADLE_HOME=/Users/mkyong/gradle
export M2_HOME=/Users/mkyong/apache-maven-3.1.1

# Apache Ant
export ANT_HOME=/Users/mkyong/apache-ant-1.9.4

# Export to PATH
export PATH=$PATH:$GRADLE_HOME/bin:$M2_HOME/bin:$ANT_HOME/bin

Test it again, now, you can access the ant command everywhere.


$ cd ~
$ pwd
/Users/mkyong

$ ant -v
Apache Ant(TM) version 1.9.4 compiled on April 29 2014
Trying the default build file: build.xml
Buildfile: build.xml does not exist!
Build failed

Done.

References

  1. Apache Ant download page
  2. How to install Apache Maven on Mac OSX
  3. Linux : gzip a folder

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Vince
Guest
Vince

Okay, it took me a good 20min to figure out that your line “ant -v” should really be “./ant -v” since the command is not yet in your path..

Lacas
Guest
Lacas

thanks sir for a noob like me… you saved my life

BringIt
Guest
BringIt

Thank. Saved my life

VAIBHAV REDDY GUDDETI
Guest
VAIBHAV REDDY GUDDETI

Thanks for the useful info :)

Vince
Guest
Vince

Also, homebrew just did everything in a minute, I should have done that to start with

cindie marren
Guest
cindie marren

THANK YOU SO MUCH!!!! I was absolutely beside myself trying to install this. Thank you for taking the time to make this!!

Md. Fakhrul Alam Onik
Guest
Md. Fakhrul Alam Onik

It shows the following error message: “Error opening archive: Failed to open ‘apache-ant-1.9.9/bin/ant'”. How should I proceed? I am a very first user

quikbeam
Guest
quikbeam

I had to create a new .bash_profile on my mac and after doing so I was getting -bash: ls: command not found

If you face problems with your command line after you do this you’ll need to restore your $PATH variable (add your bin locations back)

PATH=/usr/bin:/usr/sbin:/bin:/sbin

Bruce
Guest
Bruce

Thank you sir for that very good tutorial. Very straight forward. You even showed the “pwd” structures, awesome for new people.

Ishita Dasgupta
Guest
Ishita Dasgupta

it keeps saying:

build.xml doesn’t exist.
Build failed!

Could you please guide me towards what’s wrong!

mkyong
Guest
mkyong

It also means the “ANT” is installed successful, at least the command is executed, just cant find the “build.xml” file. You can start to create a Java project and build.xml.

For new project, advice you choose Maven or Gradle.

Avinash Sharma
Guest
Avinash Sharma

hello mkyong I have a question about the automate login to a website using java. I am trying that feature to login to http://www.craigslist.org but it shows HTTP error code like 404 and 400. Is there some problem with cookies?

Dibyendra Hyoju
Guest
Dibyendra Hyoju

You must use ant -version instead of ant -v.