How To

Install Apache Ant on Ubuntu 24.04 / Debian 13

Apache Ant is a Java-based build tool maintained by the Apache Software Foundation. It uses XML configuration files (build.xml) to define build targets, compile Java source code, run tests, package JARs, and automate repetitive development tasks. Ant has been a staple in Java development for over two decades and remains widely used in legacy and enterprise projects.

Original content from computingforgeeks.com - post 5509

This guide covers two methods to install Apache Ant on Ubuntu 24.04 and Debian 13 – the quick way from the system package manager, and the manual method to get the latest Apache Ant 1.10.15 release directly from the Apache download mirrors. We also walk through environment variable configuration, a basic build.xml project, common Ant tasks, JUnit integration, and a comparison with Maven and Gradle.

Prerequisites

  • A server or workstation running Ubuntu 24.04 LTS or Debian 13
  • Root or sudo access
  • Java Development Kit (JDK) 8 or later installed – Ant 1.10.15 requires Java 8 at minimum. If you need Java, follow our guide on installing OpenJDK 21 on Ubuntu or installing Java on Debian
  • Internet connectivity to download packages

Confirm Java is installed before proceeding:

java -version

The output should show your installed JDK version:

openjdk version "21.0.6" 2025-01-21
OpenJDK Runtime Environment (build 21.0.6+7-Ubuntu-124.04.1)
OpenJDK 64-Bit Server VM (build 21.0.6+7-Ubuntu-124.04.1, mixed mode, sharing)

Step 1: Install Apache Ant from APT (Quick Method)

The fastest way to get Ant running is through the default system repositories. The version shipped with Ubuntu 24.04 / Debian 13 is older than the latest upstream release, but it works fine for most build tasks.

sudo apt update
sudo apt install -y ant

Check the installed version:

ant -version

You should see the repository version confirmed:

Apache Ant(TM) version 1.10.14 compiled on August 16 2023

If the repository version is sufficient for your project, you are done. For the latest release (1.10.15), continue to Step 2.

Step 2: Install Latest Apache Ant 1.10.15 from Apache (Manual Method)

The manual installation gives you the latest Ant 1.10.15, released on August 29, 2024. This method works on any Linux distribution and gives you full control over the installation path and version.

First, remove the APT version if you installed it in Step 1 to avoid conflicts:

sudo apt remove -y ant

Download the latest Ant binary archive from the official Apache mirrors:

cd /tmp
wget https://dlcdn.apache.org//ant/binaries/apache-ant-1.10.15-bin.tar.gz

Extract the archive to /opt where third-party software typically lives:

sudo tar -xzf /tmp/apache-ant-1.10.15-bin.tar.gz -C /opt/

Create a symlink for easier version management. When a new Ant version is released, you just update the symlink:

sudo ln -sfn /opt/apache-ant-1.10.15 /opt/ant

Step 3: Configure Environment Variables for Apache Ant

Ant needs two environment variables – ANT_HOME pointing to the installation directory and the bin directory added to your PATH. Create a profile script so these are set for all users on login:

sudo tee /etc/profile.d/ant.sh > /dev/null << 'EOF'
export ANT_HOME=/opt/ant
export PATH=$ANT_HOME/bin:$PATH
EOF

Load the variables into your current session:

source /etc/profile.d/ant.sh

Step 4: Verify Apache Ant Installation

Confirm that Ant is accessible and reports the correct version:

ant -version

The output should show version 1.10.15:

Apache Ant(TM) version 1.10.15 compiled on August 25 2024

Check that ANT_HOME is set correctly:

echo $ANT_HOME

This should return the installation path:

/opt/ant

Verify the ant binary resolves to the correct location:

which ant

Expected output:

/opt/ant/bin/ant

Step 5: Create a Basic build.xml Project

Ant uses build.xml as its default build configuration file. Each build file contains a project with one or more targets. A target is a sequence of tasks - compile code, copy files, create JARs, run tests, and so on.

Create a sample Java project directory:

mkdir -p ~/ant-demo/src
cd ~/ant-demo

Create a simple Java source file:

cat > ~/ant-demo/src/HelloWorld.java << 'EOF'
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello from Apache Ant build!");
    }
}
EOF

Now create the build.xml file in the project root:

cat > ~/ant-demo/build.xml << 'EOF'



    
    
    

    
        
        
    

    
        
        
    

    
        
        
            
                
            
        
    

    
        
    


EOF

Run the default target:

cd ~/ant-demo
ant

Ant processes the dependency chain (compile, then jar, then run) and executes the application:

Buildfile: /root/ant-demo/build.xml

compile:
    [mkdir] Created dir: /root/ant-demo/build
    [javac] Compiling 1 source file to /root/ant-demo/build

jar:
    [mkdir] Created dir: /root/ant-demo/dist
      [jar] Building jar: /root/ant-demo/dist/ant-demo.jar

run:
     [java] Hello from Apache Ant build!

BUILD SUCCESSFUL
Total time: 1 second

Step 6: Common Apache Ant Tasks

Ant ships with a large library of built-in tasks. Here are the ones you will use most often in Java projects.

Compile Java Sources

The javac task compiles .java files. The includeantruntime="false" attribute prevents a common warning about Ant's own classes leaking into the classpath:


    
        
    

Create a JAR File

The jar task packages compiled classes into a Java archive. Set the Main-Class manifest attribute to make the JAR executable:


    
        
    

Clean Build Artifacts

Always define a clean target that removes generated directories. This ensures fresh builds start from a known state:


    
    

Run a clean build by chaining targets:

ant clean compile jar

Copy and Move Files

Use the copy and move tasks to manage files during the build process. This is common for copying configuration files or resources into the build output:


    

Execute Shell Commands

The exec task runs external commands from within Ant. Useful for tasks that do not have a built-in Ant equivalent:


    
    
    
    

Step 7: Integrate Apache Ant with JUnit for Testing

Ant integrates with JUnit through the junit and junitreport tasks. This requires the JUnit JAR and the Ant JUnit optional task library.

Download the JUnit 4 JAR and the Hamcrest dependency into a lib directory. If you are working with Gradle or Maven, dependency management is handled automatically, but Ant requires manual library management:

mkdir -p ~/ant-demo/lib ~/ant-demo/test
cd ~/ant-demo/lib
wget https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar
wget https://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar

Create a simple test class:

cat > ~/ant-demo/test/HelloWorldTest.java << 'EOF'
import org.junit.Test;
import static org.junit.Assert.*;

public class HelloWorldTest {
    @Test
    public void testMain() {
        // Verify the main method runs without exceptions
        HelloWorld.main(new String[]{});
        assertTrue("HelloWorld executed successfully", true);
    }
}
EOF

Add a test target to build.xml. The key settings are haltonfailure to stop the build if tests fail, and printsummary to show results in the console:


    
        
            
            
        
    



    
        
            
            
        
        
        
            
        
    

Run the tests:

ant test

A successful run shows the test summary with no failures:

Buildfile: /root/ant-demo/build.xml

compile:

compile-tests:
    [javac] Compiling 1 source file to /root/ant-demo/build

test:
    [junit] Running HelloWorldTest
    [junit] Hello from Apache Ant build!
    [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.05 sec

BUILD SUCCESSFUL
Total time: 1 second

Step 8: Apache Ant vs Maven vs Gradle

Choosing between Ant, Maven, and Gradle depends on your project requirements, team preferences, and whether you need dependency management built in. Here is a direct comparison across the areas that matter most in production Java projects.

FeatureApache AntApache MavenGradle
Configuration formatXML (build.xml)XML (pom.xml)Groovy/Kotlin DSL
Dependency managementManual (or via Ivy)Built-in (Maven Central)Built-in (Maven/Ivy repos)
Convention vs flexibilityNo conventions - full controlConvention over configurationConvention with flexibility
Build speedFast (no overhead)ModerateFast (incremental + daemon)
Learning curveLow for basic tasksModerateSteeper (DSL syntax)
Best forLegacy projects, custom workflowsStandard Java/enterprise projectsMulti-module, Android, polyglot
Plugin ecosystemExtensive optional tasksHuge plugin ecosystemGrowing plugin marketplace

When to use Ant: You have an existing Ant-based project, you need full control over the build process without convention constraints, or you are working with a simple project that does not need dependency management. Ant combined with Apache Ivy also provides dependency resolution if needed.

When to use Maven or Gradle instead: New Java projects benefit more from Maven or Gradle because dependency management is critical for modern development. Maven is the safer choice for enterprise teams that value standardization. Gradle is better for large multi-module builds and Android development.

Conclusion

Apache Ant is installed and configured on your Ubuntu 24.04 or Debian 13 system. The manual installation method gives you the latest 1.10.15 release with full control over upgrades, while the APT method is quicker for environments where the exact version does not matter. You have a working build.xml template, JUnit test integration, and an understanding of where Ant fits compared to Maven and Gradle.

For production CI/CD pipelines, integrate Ant builds into Jenkins or your preferred CI tool. Keep Ant updated by replacing the extracted directory under /opt and updating the symlink - no package manager needed.

Related Articles

Databases Manage MySQL / PostgreSQL / SQL Server using SQLPad Editor Databases Configure MariaDB Master-Master replication on Ubuntu 22.04|20.04|18.04 Ubuntu Install Asterisk PBX on Ubuntu 24.04 / Debian 13 Debian Install and Configure Telegraf on Ubuntu / Debian

Leave a Comment

Press ESC to close