Getting started with Ant Each one of us has used Ant knowingly or unknowingly, while building our projects. But, most of us are not aware of its existence. Thanks to IDEs like Eclipse & NetBeans !! But, I feel that one must know how things happen. How a series of commands are executed when you build your project and how you can control and alter this flow. This entry is not meant to give you insight into the build process but it is meant to introduce the tool (Ant) to beginners like me. We will go through installation and normal usage of Ant Tool. So, lets get started :-) What is ANT? Ant is a Java-based build tool used for automating the build process. Ant is written in Java and that's what makes it platform-independent. This also means that you lose out on doing certain things (issuing certain OS-specific commands in build.xml) that could be done fastly in a OS-dependent manner. ANT is an acronym for "Another Neat Tool". :-) It accepts input in form of XML files (like build.xml) and can be used to perform repeatitive task in an efficient manner. Installing ANT Download the binary distribution. Now, all you have to do to install ant and run from command line is to set following environment variables:
And Bingo !! you are done with installation. It's time to test whether we have installed Ant correctly or not Test Your Installation To test whether you have successfully installed Ant or not, type >ant on command line and press enter It will give an error like Buildfile: build.xml doesn't exist! Build failed because right now there is no build.xml file existing in the current directory. Ant tool makes use of build.xml to automate the build process. Congratulations !! You have successfully installed ANT if you can see this error message. :-) What is build.xml ? It is nothing but a normal XML file that is used to automate the build process. It contains certain commands that are executed in a defined/prescribed manner. You can think of it as writing a Shell Script which will be executed by Ant tool. Actually it's more like a make-file that we have in Linux/Unix environment. A normal build.xml file would consist of following sections, in that order:
All these steps are not necessary and it depends entirely on you that what you wanna keep in your build.xml. A look at a simple build.xml would simply what seems so tough right now. :-) BUILD.XML Lets see what it really means:
First line tells us that it is a XML file. Then comes the root element, project. Rest of the code for this project will come in between project tags. It has 3 attributes:
These 2 lines declare user-defined variables which are used later in the file. src holds the location of the current directory as given by '.' build holds the location "basedir/build" where basedir was defined in project name above.Remember all the paths mentioned are relative to basedir So, property tag is used to give name-value pairs, but same effect could have been achieved by mentioning these name-value pairs in an external file (named build.properties) and using them in build.xml as they are used now.
Target tag is like a function which you can call again to perform a reusable task. These lines are pretty simple to understand. The echo tag will echo/display whats written to the output console, and the delete command will delete the build directory ${build} is way to evaluate the build variable that we have defined above using property tag.
Here, we just create a directory using mkdir command. The thing to note here is the depends attribute of target tag. This attribute is used throughout build.xml to create a chain of targets, to give a sequential flow i.e. it helps in specifying the dependencies of one module over another and tells us how the targets will be called.
This function compiles the source files present in src directory and stores the resulting .class files in build directory. I have deliberately not added more steps like creating a war file and deploying it on server, because that would make this entry long and complicated un-neccessarily. Time to run what we have done so far. Running this script To run this script, create a folder and name it Test and copy this build.xml file in it. And to test whether your script works or not, create any correct java file in this Test folder, which we will compile. Your directory structure should look like this. Test | |------ build.xml | |-------HelloWorld.java To run your build script, go to Test Folder, and type ant C:\Users\Agraj\Desktop\Test>ant If everything goes correct, it will give output Buildfile: build.xml clean: [echo] Deleting build [delete] Deleting directory C:\Users\Agraj\Desktop\Test\build init: [echo] Creating new directory: build [mkdir] Created dir: C:\Users\Agraj\Desktop\Test\build compile: [echo] Compiling source files present in . to build [javac] Compiling 1 source file to C:\Users\Agraj\Desktop\Test\build BUILD SUCCESSFUL Total time: 2 seconds Having said that, the problem that occurs most commonly is that Ant tool is unable to parse the build.xml file correctly. This is so because even a space or tab inserted in xml file can cause Ant to fail. You can get to know about the extra spaces/tabs using vi editor on linux/unix machines, but it can be pretty frustating on windows to look out for such an error. Although, most of us will never change the build.xml while development but still it helps a lot when you know what happens when you build your project and you don't feel alien to the environment :-) Random Notes:
Check out our Related Courses Weblogic Tutorials Introduction to Clustering Weblogic WebLogic Server Admin Training in Bangalore Oracle Weblogic Server Installation
You liked the article?
Like : 0
Vote for difficulty
Current difficulty (Avg): Medium
1/15
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.
Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox