ANT


What is ANT?

Apache Ant is a Java based build tool from Apache Software Foundation. Ant is written in Java. Apache Ant's build files are written in XML and describe how to build a project. Ant was designed to be an extensible tool to automate the build process of a Java development project.

What is build process? 

It is the different steps to perform before deploying the application to a production environment. (compile the source code, validation of the source code with test, packaging the components of the application and deploying the software). Manage the build process is prone error and very repeatable. Using an automated building process makes it much easier to control the building and deployment of java projects.

In this article, I will show you how to use Ant to compile and run a simple java project. 

Environment setup:

To install Ant on windows, take the following steps:

1.  Download the binary distribution zip file from http://ant.apache.org/ 

2. unzip the jar file in the directory that you want. For example, C:\Program Files\apache-ant-1.9.13 

3. Setup the environment variable for ANT by adding ANT_HOME.

Ant home4. Add ant's bin directory in the Path variable

Ant bin

5. Test that Ant is installed by typing the command ant -version

Ant test

Creating the project directory:

Create a java project using IDE (Eclipse, Intellij). In the src directory, create Main.java class as following.

 

Writing the build file: 

We will create the build file which is and XML file, called here build.xml. It describes the actions to create directories, compile the project, make jar files and run the application.

 

a. Create directories:

- Create the directories build/classes which will contain the compiled classes. Also, create dist directory which contains redistributable artifacts of the project

 

b. Compile, archive and clean

b.1. The target "compile" compiles the java files in the src directory and add the compiled files .class in the build/classes" directory.

 

b.2. The target '"archive" creates the jar file containing all the classes in the build/classes directory 

 

b.3. The target "clean" deletes the directories build and dist generated in the targets init and filled in the targets (compile and archive)

 

Note: Ant uses <depends> to declare the dependencies of the current target to one or more other targets. The target compile will not run if the target init is not executed.

d. Add an execute target to run the Main class using the compiled classes in the directory build/classes

 

Following is the complete build file.

 

Execution: 

  1. To execute the ant file, you can run the project using the command line : ant execute.
  2. You can also run the project from the IDE.

 

Runfromide

The output of this command:

Antresult

References:

Steve Loughran; Erik Hatcher. Ant in ActionManning Publications, 2007

Apache Ant Tutorial. /www.tutorialspoint.com/an

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Add a comment