How to use Jenkins with Ant to build your Java apps

Jenkins builds with ANT

Maven and Gradle tend to get all of the big headlines these days when it comes to the world of Java builds, but Apache Ant should never be discounted. With ongoing updated to the code base and an established history of reliable use in a vast array of Java projects, Ant will remain a popular build tool for years to come. As such, the ability to integrate Apache Ant with Jenkins and other popular DevOps tools is of key importance. In this quick tutorial, we’ll show you how to run Apache Ant builds with Jenkins.

Ant and Jenkins prerequisites

To follow this tutorial, you will need the following tools installed:

  • Apache Ant installed and added to the PATH
  • Jenkins running on Java 8 or newer
  • The Apache Ant Jenkins plugin installed
  • A local Git installation to pull from GitHub

This tutorial was created with Jenkins and Ant installed on an Ubuntu 20 server, but the steps are the same for both Windows and MacOS.

Build jobs in Jenkins with Ant

There are three different approaches to build Java projects in Jenkins with Apache Ant:

  1. Create a Jenkins Ant build job with the web interface
  2. Write an Apache Ant build Jenkinsfile
  3. Use a Docker container for your Jenkins Ant build

This article will focus on options 1 and 2. The second part of this article examines how to perform a Jenkins Docker Ant build.

Jenkins Ant and the web UI

The Jenkins Ant plugin is part of the default installation, and as such is available to any Freestyle project created in the CI tool.

To take advantage of the Jenkins Ant plugin, create a freestyle project named ‘Apache Ant and Jenkins Build Job Example.’

Specify the following GitHub URL as the Git repository location and point to either the master or main branch:

https://github.com/learn-devops-fast/rps-ant.git

Add an “Invoke Ant Target” build step and specify the following Apache Ant tasks for Jenkins to perform:

clean compile test package war

Run the Jenkins Ant build job and the source code will be pulled from GitHub, the tasks will run, and the application will be compiled, tested and packaged as a WAR file.

jenkins ant build job

Here is how to use the Jenkins Ant plugin to build Java projects.

Jenkins pipeline Ant build example

As an alternative the the web interface, many developers to code their builds as a Jenkins Ant pipeline. If a pipeline build was chosen instead of a freestyle project, the following Ant based Jenkinsfile be sufficient to build the project:

Your intro to GitHub Actions training course

Here’s how to get started with GitHub Actions:

Follow these tutorials to learn GitHub Actions fast.

pipeline {
  agent any
  stages {
    stage('Log Ant version info') {
      steps {
        sh 'ant -version'
      }
    }
    stage('GitHub Jenkins Ant Build') {
      steps {
        git 'https://github.com/learn-devops-fast/rps-ant.git'
        sh 'ant clean compile test package war'
      }
    }
  }
}

When this pipeline runs, the effect is exactly the same as with the Jenkins Ant freestyle project. The code will be pulled from GitHub, the Ant tasks will run, and the required artifacts will be assembled ands stored in the Jenkins workspace for when they are required.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close