Igor Khromov blog

Install Apache Ant on macOS with brew

1.1. Install latest version

Before install we can check what the latest version available in brew:

brew info ant

You will see the output with formula details like this:

ant: stable 1.10.5, HEAD
Java build tool
https://ant.apache.org/
/usr/local/Cellar/ant/1.10.5 (1,651 files, 37.4MB) *
  Built from source on 2019-01-22 at 16:45:43
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/ant.rb
==> Requirements
Required: java >= 1.8 ✔
==> Options
--HEAD
	Install HEAD version
==> Analytics
install: 9,502 (30 days), 27,541 (90 days), 140,172 (365 days)
install_on_request: 6,813 (30 days), 22,238 (90 days), 104,256 (365 days)
build_error: 0 (30 days)

If you ok with current version (line #1 stable 1.10.5) you can execute install command:

brew install ant

You will se output like this:

==> Downloading https://www.apache.org/dyn/closer.cgi?path=ant/binaries/apache-ant-1.10.5-bin.tar.xz
Already downloaded: /Users/ikhromov/Library/Caches/Homebrew/downloads/2d9de475d484b0b09e19cf0240d5861788e0db04cd088ef94b4af0086735c1a7--apache-ant-1.10.5-bin.tar.xz
==> Downloading https://www.apache.org/dyn/closer.cgi?path=ant/ivy/2.4.0/apache-ivy-2.4.0-bin.tar.gz
==> Downloading from http://apache.volia.net/ant/ivy/2.4.0/apache-ivy-2.4.0-bin.tar.gz
######################################################################## 100.0%
==> Downloading https://www.apache.org/dyn/closer.cgi?path=commons/bcel/binaries/bcel-6.3.1-bin.tar.gz
==> Downloading from http://apache.cp.if.ua/commons/bcel/binaries/bcel-6.3.1-bin.tar.gz
######################################################################## 100.0%
🍺  /usr/local/Cellar/ant/1.10.5: 1,653 files, 39.3MB, built in 24 seconds

1.2. Install Ant library dependencies (sshexec)

Sometimes we need to execute some commands on the remote host (for example AWS EC2 instance) with ssh. Ant has a special task for that: sshexec. We can make a very simple task in a build.xml to create a folder in some location (IP address and all credentials used just for demonstration purposes, replace them if you want to execute it):

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" basedir="." default="deploy-prod">

	<property name="host" value="0.0.0.0" />
	<property name="username" value="ubuntu" />
	<property name="keyfile" value="~/ec2-host.pem" />
	<property name="build.folder" value="/opt/folder" />

	<target name="deploy-prod">

		<sshexec host="${host}"
		         username="${username}"
		         keyfile="${keyfile}"
		         trust="true"
		         verbose="true"
		         command="mkdir ${build.folder}" />

	</target>

</project>

You will see the output like this:

BUILD FAILED
/Users/username/deploy-prod.xml:15: Problem: failed to create task or type sshexec
Cause: Could not load a dependent class com/jcraft/jsch/Logger
       It is not enough to have Ant's optional JARs
       you need the JAR files that the optional tasks depend upon.
       Ant's optional task dependencies are listed in the manual.
Action: Determine what extra JAR files are needed, and place them in one of:
        -/usr/local/Cellar/ant/1.10.5/libexec/lib
        -/Users/username/.ant/lib
        -a directory added on the command line with the -lib argument

Do not panic, this is a common problem.
The commonest cause is a missing JAR.

This is not a bug; it is a configuration problem


Total time: 1 second

So how to fix this? Actually it’s very easy. If we check sshexec task official page, then we can found a note with some details.

Note: This task depends on external libraries not included in the Ant distribution. See Library Dependencies for more information. This task has been tested with JSCh 0.1.29 and above and won’t work with versions of JSCh earlier than 0.1.28.

To add JSCh library to Ant, we need to download it from library official site.

You can open the link and find the Download section, copy a link to the latest version available (for April 2019 it’s 0.1.55) and download JAR file to Ant proposed location (line #7 in notification output):

wget -O jsch-0.1.55.jar -P=/usr/local/Cellar/ant/1.10.5/libexec/lib/ https://sourceforge.net/projects/jsch/files/jsch.jar/0.1.55/jsch-0.1.55.jar/download

2. Install specific version

To check if specific version of Apache Ant is available in brew we can run search command:

brew search ant

You will see the output like this:

==> Formulae
ant ✔                       antlr                       dante                       howard-hinnant-date         pngquant
ant-contrib                 antlr4-cpp-runtime          enchant                     libantlr3c                  quantlib
ant@1.9                     antlr@2                     fantom                      libimagequant               smartypants
antigen                     anttweakbar                 geant4                      libquantum                  tarantool
antiword                    ctags-exuberant             homeassistant-cli           plantuml                    vagrant-completion

We can see (line #4) that version 1.9 also available for install. To install it we can run:

brew install ant@1.9

3. Switch between versions

In a case when we have two versions available we can switch to use one of them with brew:

brew unlink ant
brew link --force ant@1.9

Now you can check that if 1.9 is current one:

ant -version

Output will be like this:

Apache Ant(TM) version 1.9.13 compiled on July 10 2018