How to Build Java Projects with Apache ANT

In this tutorial, i will show you how to build your Java Application with Apache Ant. At IDR Solutions 90% of our test is automated and to do so will need an automated means of build our project. This is when Apache Ant comes in handy. There are several types of Java build tools such as Maven, Gradle etc. Each of this build tools have their strengths and weaknesses.

What is Apache Ant

As I explained above, it is an automated build tool for Java software build processes and also helps you manage your project from command line.

Prerequisite

  1. You need to have Apache installed. You can get it from here
  2. You need to have JDK installed. You can get it from here
  3. Have a Java IDE installed (I will be using NetBeans )

Open your IDE and Create a new Java Project. (I will call mine AntProject)

Screen Shot 2015-07-13 at 14.42.25

Note

When you create a NetBeans Project, it automatically generates an Ant file called build.xml which will compile, clean and build your project for you. I will go ahead and show you how you can create the build file your self so you can do a whole lot of customisation to your project.

In Netbeans, switch to the File view and create a new xml file. I will call mine jbuild.xml.

Screen Shot 2015-07-13 at 14.52.24
Delete the root tags and add project and description tag to the xml file so it looks like mine.

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<project name="AntProject" default="main" basedir=".">
<description>
Description of your project
</description>
</project>

Screen Shot 2015-07-15 at 10.23.03
We will give our project a name.

<property name="projectName" value="antproject" />

Lets go ahead and set some properties to some values for our directories

<!--Staging Directory --> 
<property name="staging.dir" location="staging" />
 
<!-- Java sources Directory -->
<property name="src.dir" location="src" />
 
<!-- Java classes Directory-->
<property name="build.dir" location="${staging.dir}/bin" />
 
<!-- Output Jar Directory -->
<property name="dist.dir" location="${staging.dir}/dist" />

We will now create our first target called which will be the point of entry for Ant. i will call mine main. This target will depend on three other targets, meaning the three targets will be executed before the main target.

<target name="init">
    <target name="main" depends="clean, compile, dist" />
</target>

Create the clean target. The clean target will delete the staging directory

<target name="clean" description="Flush staging directory">
    <delete dir="${staging.dir}" />   
</target>

Create compile target, This will compile the source code and place it in the build directory

<target name="compile" depends="init" description="compile the source ">
    <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" />
</target>

We will not create the dist target which will depend on compile. This will package our JAR

<target name="dist" depends="compile" description="package, output to JAR">
 
    <mkdir dir="${dist.dir}" />
 
    <jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}" >
        <manifest>
            <attribute name="Main-Class" value="${projectName}/AntProject" />
        </manifest>
    </jar>
</target>

Notice we placed a manifest tag in the jar tag.This will adds meta-data about the .jar file and its content and specify the main class.

Now lets put all together.

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<project name="AntProject" default="main" basedir=".">
    <description>
        Description of your project
    </description>
 
 
 
<property name="projectName" value="antproject" />  
 
<!--Staging Directory --> 
<property name="staging.dir" location="staging" />
 
<!-- Java sources -->
<property name="src.dir" location="src" />
 
<!-- Java classes -->
<property name="build.dir" location="${staging.dir}/bin" />
 
<!-- Output, Jar -->
<property name="dist.dir" location="${staging.dir}/dist" />
 
<target name="main" depends="clean, compile, dist" />
 
<target name="init">
    <mkdir dir="${staging.dir}" />
    <mkdir dir="${build.dir}" />
</target>
 
<target name="clean" description="Flush staging directory">
    <delete dir="${staging.dir}" />   
</target>
 
 
<target name="dist" depends="compile" description="package, output to JAR">
 
    <mkdir dir="${dist.dir}" />
 
    <jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}" >
        <manifest>
            <attribute name="Main-Class" value="${projectName}/AntProject" />
        </manifest>
    </jar>
</target>
 
</project>

Now let head over to NetBeans. Create a bash file if you are on Linux/MAC or Batch if on Windows.
Replace the path of ANT_HOME to where you installed Ant Paste this code to the bash file

#!/bin/bash
#1.4
 
export ANT_HOME=/PDFdata/library/apache_ant/
export PATH=$ANT_HOME/bin:$PATH
 
ant -buildfile jbuild.xml main
if [ $? -eq 1 ]; then
echo "Failed on Build"
exit 1
fi

if on Windows, click on Start –> Right click on computer –> Advance settings –> Environmental Variables and set variables for ANT.

Capture
Before you add the path to the existing path, make sure the exiting path has a semicolon at the end, if not add one before you paste the path.
Capture1

Once done open command line and type in ant. You should see something like this.

Capture2
Create a batch file and paste this code

@ECHO OFF
 
ant -buildfile jbuild.xml main
IF ERRORLEVEL 1 (
echo "Failed build"
exit 1
)

As you can see in both scripts, we call ant then specify the build file (jbuild.xml – the one we created) then we call the main target from the build. You can also run without calling the main target as we specified it as default in our project tag.
Your project should be looking like this with either bash or bat file.

Screen Shot 2015-07-15 at 12.19.27
Now open command line and navigate to the AntProject directory.
On MAC/LINUX, type in chmod +x build.bash hit enter then ./build.bash and enter
On Windows, type in build.bat and hit enter
You should now see all the build steps in your console and you build should be Successful.

Screen Shot 2015-07-15 at 12.29.27

Head over to NetBeans and you should see a staging directory which contains the compiled class in bin folder and jar in dist folder.

Screen Shot 2015-07-15 at 12.34.14

Hopefully you have found this quick guide useful. In the next article, I will show you how to setup Jenkins which is an open source continuous integration tool. We will then use both to perform some automated static Analysis on our code base.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Related Posts:

The following two tabs change content below.

Ernest Duodu

Developer at IDRSolutions
Ernest is a developer at IDRSolutions. He focuses mainly on our JavaFX PDF Viewer implementation. He was a speaker at JavaOne 2014, where he did a joint session titled "Lessons learnt developing a NetBeans PDF viewer plugin in JavaFX". Aside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.
Ernest

About Ernest Duodu

Ernest is a developer at IDRSolutions. He focuses mainly on our JavaFX PDF Viewer implementation. He was a speaker at JavaOne 2014, where he did a joint session titled "Lessons learnt developing a NetBeans PDF viewer plugin in JavaFX". Aside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>