Get started Bring yourself up to speed with our introductory content.

A declarative Jenkins pipeline for Ant builds on Docker example

Ant and Docker in a Jenkins pipeline

There are many ways to approach a Jenkins build with Apache Ant, but by far the most manageable and maintainable integration path is to delegate to a Docker container and remove the Jenkins administrator from the burden of having to maintain all of the various resources need to perform a complex build job. In this example, we will show you just how easy it is to build with Apache Ant, Docker and a Jenkins pipeline.

One challenge you encounter when creating a Docker based, Jenkins pipeline for your Apache Ant builds is the fact that there is not official container available at Dockerhub. Official containers exist for Maven and the JDK, but not for Apache Ant, so you need to search for an existing one you can use, or build your own. For this Jenkins, Ant and Docker example, I composed my own and made it publicly available. It can be pulled through the following command:

docker push cameronmcnz/ant-jdk8-git:latest

If you were to look at the Dockerfile used to build this container, you’d see that it contains Git, Ant and version 8 of the JDK.

Ant on Docker Jenkinsfile example

With the path to the container on hand, the creation of a Jenkins pipeline with Ant and Docker is a relatively simple task. The Jenkinsfile is written as though you were to run the Ant build locally, with the only difference being the reference at the start of the Jenkins pipeline to the Docker agent. Here’s how the full Jenkinsfile that runs an Apache Ant build on Docker looks:

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 { docker { image 'cameronmcnz/ant-jdk8-git:latest' } }
  stages {
    stage('Log the Jenkins Docker Ant Git and Java version info') {
      steps {
        sh 'ant -version'
	sh 'java -version'
	sh 'git --version'
    stage('GitHub Jenkins Ant Docker Build') {
      steps {
        git ''
        sh 'ant clean compile test package war'

When this Jenkins pipeline runs, the Docker agent pulls from GitHub, builds the Java web application with Apache Ant and then copies all of the build artifacts to the local Jenkins workspace on the host machine.

Jenkins Ant Docker Pipeline

When the Jenkins Ant pipeline runs, the Docker container builds the Java app successfully.

Docker and Apache Ant Dockerfile

For those interested in how the Apache Ant Docker container was built, here is the contents of the Dockerfile:

FROM alpine:3.5

#Create Ant Dir in Docker container
RUN mkdir -p /opt/ant/
#Download Ant 1.9.8
RUN wget -P /opt/ant
#Unpack Apache Ant for Java builds
RUN tar -xvzf /opt/ant/apache-ant-1.9.8-bin.tar.gz -C /opt/ant/
# Remove tar file
RUN rm -f /opt/ant/apache-ant-1.9.8-bin.tar.gz
#Add Sonarqube lib
RUN wget -P /opt/ant/apache-ant-1.9.8/lib/
#Install JDK 1.8
RUN apk --update add openjdk8
#Install GIT for GitHub pulls
RUN apk --update add git
#Install Curl
RUN apk --update add curl
#Set Ant Home in Docker container for Jenkins
ENV ANT_HOME=/opt/ant/apache-ant-1.9.8
#Set Ant Params in Docker for Jenkins
ENV ANT_OPTS="-Xms256M -Xmx512M"
#Update Path
ENV PATH="${PATH}:${HOME}/bin:${ANT_HOME}/bin"