Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Tuesday, August 10, 2021

How to build Java Projects using Apache ANT ? Beginner Tutorial with Example

Apache Ant tutorials for beginners

Hi Guys, today I would like to share some of my experience while working with the ant build tool in form of short ant tutorials, ANT is one tool that has gained its place almost everywhere, it’s an integral part of any project’s build system and provides the foundation for a complex, extensible build environment. I have been using ANT since my early programming days and I have seen some of the best-designed build environments around ANT. 

In these ant tutorials for beginners we will see what is ant build tool? How to use ANT for building project? Fundamentals of ANT build tool, setting up ant build environment etc. 

I have created a series of ant tutorials and you can check next part of ant tutorials by following links ant tutorial part 2 and ant tutorial part 3

ANT has rich set of task for almost everything e.g. svn , cvs to check out code , junit to test the code , jar ,zip to create deliverable , compile , run, debug , copy,delete almost everything. Without ANT build tool you can not understand complex built systems which are based on XML and properties files.

To understand any complex build system we need to keep fundamentals in mind, fundamentals of property file, fundamentals of XML, fundamentals of ant and workflow and this is where this ant tutorial will help you to understand the fundamentals involves in ant build systems.

What is Apache Ant?

Ant is a build tool used for building java based projects and has been developed by Apache foundation. It’s also called Apache ant many times, you can download ant by following link ant download.  The ANT build tool is released as open-source and anybody can use it, it’s free like java.

How to setup ANT build environment

In this part of ANT tutorial we will see how to setup? Once you downloaded ANT you need setup PATH and Java Classpath . Binaries of ant must be in PATH so that you can build any project by executing “ant” command from command prompt and lib of ant must be in classpath. 

Once you have "ant" in your path you can execute command "ant" from command line. When you issue "ant" it will look file called "build.xml" in current directory and execute it target by target.

Fundamental of ANT property attribute

ANT tutorial will not complete until we cover the fundamentals of property. To starts with lets fist start with property file, concept of property file is very old, which was I believe created a new era of configuration. It’s a simple text file which holds properties which has key and value e.g.

build. properties

Here is a property file, build.major.number is one property whose key is "build.major.number" and value is "1".You can load this file in ant and can use its property by using ant's core task called “property” , I am not covering much on this tutorial as it is an ANT tutorial.

ANT tutorial: XML fundamentals for ANT

ant tutorials, ant build tutorial, ant tutorial for beginnersIn this part of ANT tutorial we will see some basics of XML because XML is used to write ANT build file. Property file has limitation that it’s doesn't provide hierarchical functionality, without that you can not group things, XML solves that and evolved the world of configuration. In XML we have elements and attributes for example

here”<" is opening tag”  "/>" is closing tag,”project" is an element and "name" is an attribute whose value is "test". You can either use "/>" for closing tag or you can have .

First element of any XML file is called root element. "." is used to denote current directory and ".." is used to denote parent directory

ANT tutorial and fundamentals for beginners

ant works on concept of build file, before using ant I used to have batch file in DOS and shell scripts in Linux for doing my build related things, once I come to know ANT I realized its a great tool and since it is based on java and XML its platform independent so you can write one build.xml and can build your project on any 
Environment e.g. either UNIX or Windows provided you have installed ANT and has correctly setup ANT_HOME etc.

The first or root element of any build file is always the tag.
Its must have element and one build file can have only one project element.

the tag has three attributes: name, default, and basedir.

·         The name attribute is used to define project name
·         The default attribute is a target you have defined in you build.xml file . If you run Ant without specifying a target on command line, Ant executes the default target. If the default target doesn't exist, Ant returns an error.
·         The basedir attribute defines the root directory of a project. Typically, it is ".", the directory in which the build file resides; regardless of the directory you're in when you run Ant.

Now here comes two important concept of ant "target" and "task" , we can say target holds group of task e.g. target is compile which holds task for copy files ,deleting old files and compiling using javac command , here each one of these operation can be a task in ant.
·         Ant has many core tasks for common functionality e.g. etc and you can also provide your custom task by writing a corresponding java class for that. there are lots of third-party library which provides custom ant task e.g. antenna (one I used while working with J2ME) , ant-contrib ,svnant etc

Creating build.xml using ANT

In this part of ANT tutorial we will see how to create build.xml in ANT 

  //for copying file
  //for compiling
target has the following attributes: 
  • name -- the name of the project 
  • depends -- another target on which this target depends (an extremely important attribute) 
  • basedire -- base directory. 

Out of these three, most important one is the "depends" attribute it contains comma-separated other targets which tells ant that first execute those target in order they are defined and then execute me. 

 So you can say you have compile the target and before that, you want to do a cleanup of the old class file to start afresh compile. you can have a target called "clean" and let's make compile depends upon "clean" which will guarantee that the first clean will be executed and then compile.

For example, A is a piece of code that can be executed. Each ANT task is supported by java class or system commands. Common tasks are etc. I will write some more common ant tips and ant tutorials and experience around building ANT
as a build tool, I hope you find this ANT tutorial useful. Don’t forget to read the next part of the ANT tutorials:

How to write build.xml and run build in ANT
What are builtin Properties in ANT and How to fail build in ANT
10 examples of UNIX find command
10 examples of UNIX grep command


Anonymous said...

hi What is difference between Maven and ANT , I heard Maven is better than ANT what is your opinion should I use ANT in my new project or go for Maven

Raksh said...

Apache ANT and Maven both are product from Apache Software Foundation. Both Apache ANT and Maven can be used to build java application and run, though major difference between Maven and ANT is that Maven maintains dependency better. it has a Maven Repository where it stored all jars related to project, overall its much easier to maintain dependency by using Maven than ANT.

Rajeev said...

beautiful blog.Thanks

Anonymous said...

Hi, I think some lines of code from examples are missing on page ... Please verify ... Thanks

Post a Comment