Multiple Versions of Apache Ant on Ubuntu

Ubuntu Linux comes with alternatives – a command-line utility to create and maintain symbolic links to default commands. This utility can be found in many Linux distributions; however, this guide was specifically written for and tested on Ubuntu 11.10.

Here is how you can use alternatives to install and configure apache-ant.

  1. Download and extract apache-ant-1.6.5:

    wget http://archive.apache.org/dist/ant/binaries/apache-ant-1.6.5-bin.tar.bz2
    tar xvf apache-ant-1.6.5-bin.tar.bz2
  2. Repeat for apache-ant-1.8.2 (or any other version you may need):

    wget http://archive.apache.org/dist/ant/binaries/apache-ant-1.8.2-bin.tar.bz2
    tar xvf apache-ant-1.8.2-bin.tar.bz2
  3. Check if apache-ant is already installed and move its binaries to the “stock” directory:
    test -f /usr/share/ant || sudo mv /usr/share/ant /usr/share/apache-ant-stock
  4. Move apache-ant-1.6.5 to /usr/share:

    sudo mv apache-ant-1.6.5 /usr/share/
  5. Move apache-ant-1.8.2 to /usr/share:

    sudo mv apache-ant-1.8.2 /usr/share/
  6. Use alternatives to map /usr/share/apache-ant-1.6.5 to /usr/share/ant, which is Ubuntu’s default location for the apache-ant binaries:

    update-alternatives --install /usr/share/ant ant-bins /usr/share/apache-ant-1.6.5 1
  7. Add /usr/share/apache-ant-1.8.2:

    update-alternatives --install /usr/share/ant ant-bins /usr/share/apache-ant-1.8.2 2
  8. Re-add the “stock” apache-ant binaries to be provided by alternatives:

    update-alternatives --install /usr/share/ant ant-bins /usr/share/apache-ant-stock 3
  9. Use alternatives to the map /usr/share/apache-ant-1.6.5/bin/ant executable to /usr/bin/ant:

    update-alternatives --install /usr/bin/ant ant /usr/share/apache-ant-1.6.5/bin/ant 1
  10. Map the /usr/share/apache-ant-1.8.2/bin/ant executable to /usr/bin/ant:

    update-alternatives --install /usr/bin/ant ant /usr/share/apache-ant-1.8.2/bin/ant 2
  11. Add the “stock” executable to alternatives:

    update-alternatives --install /usr/bin/ant ant /usr/share/apache-ant-stock/bin/ant 3
  12. Configure alternatives to use the apache-ant-1.6.5 binary directory:

    update-alternatives --config ant-bins
  13. Configure alternatives to use the apache-ant-1.6.5 binaries to provide ant:

    update-alternatives --config ant
  14. Verify that apache-ant is properly configured for version 1.6.5:

    ant -version