Apache Ant is an open source build tool, in other words, a software tool for automating software build processes. Why? Typically, the building software processes implies much more than just writing the code and then compiling it, you might need to fetch some libraries, clean the build area, validate the source code and many others tasks. Ant is implemented using the Java language, so requires the Java platform, and is best suited to building Java projects. If a JDK is not present, only the JRE runtime, then many tasks will not work. The most immediately noticeable characteristic is that Ant uses XML to describe the build process and its dependencies, unlike other build tools, which are inherently shell-based. By default the XML file is named build.xml.
EnvironmentRequired software that has already been installed and configured on the system.
- Ubuntu 9.10
- JDK 6 Update 16
Required files (~/downloads)Downloaded files required during the installation process.
Step 1 - Unpack and move to the desired locationUnpack the archive from the folder where the apache-ant-1.7.1-bin.tar.gz file has been downloaded. Move the newly created folder (apache-ant-1.7.1) to the desired location (e.g. ~/lab/tools). Finally, create a symbolic link (e.g. ant) to facilitate further references to this folder, allowing that way a cleaner update of the Ant tool.
~$ cd downloads/ ~/downloads$ tar -zxvf apache-ant-1.7.1-bin.tar.gz [...] apache-ant-1.7.1/lib/libraries.properties apache-ant-1.7.1/lib/xercesImpl.jar apache-ant-1.7.1/lib/xml-apis.jar ~/downloads$ mkdir -p ~/lab/tools ~/downloads$ mv apache-ant-1.7.1 ~/lab/tools ~/downloads$ ln -s ~/lab/tools/apache-ant-1.7.1 ~/lab/tools/antTerminal
Step 2 - Configure environment variablesBefore we can play with Ant there is some additional configuration work. First, we must set up the ANT_HOME environment variable, which must point to the Ant's installation folder. Secondly, we must append the path to the Ant's bin folder inside the PATH environment variable. And finally, if we want to have available JDK functionalities such as for the javac task or the rmic task we must set up the JAVA_HOME environment variable (this variable has already been set in the previous installation of the Java Sun JDK 6.)
We can do this either editing the ~/.profile file or ~/.bashrc file. The difference between these files is that the former is loaded once when you log in, whereas the latter is loaded every time you start a new terminal.
Log out or close all the terminals, depending on the file edited, to apply the changes. Open a terminal and execute this command: "ant -version", this should display the version information.
# Ant environment variables export ANT_HOME=$HOME/lab/tools/ant export PATH=$PATH:$ANT_HOME/bin~/.profile
~$ ant -version Apache Ant version 1.7.1 compiled on June 27 2008Terminal
TestThis example program shows how to create and run a jar file after compiling all the java files. As previously stated Ant's buildfiles are written in XML, each buildfile contains one project and at least one target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this id.
In the above code, on line 2, "simpleAntProject" is the project name. We are also setting base directory as the current directory by just giving a dot (".") . The default attribute is used for setting the starting point for the Ant script, we are telling the script to start from "main" target name (on line 36).
The target element has the depends attribute to indicate, that particular task can be executed only after the certain number of dependencies. In our case, before running the jar file, we have to create it, and before this we have to compile the files, and before compiling we need to create the folder structure, and before this we need to clean these folders and...nothing else. So we will start from the cleaning task (line 28) and end up running the jar file (line 32).
- Clean target
- This task deletes the folder where the output will be generated. The output variable "classes.dir" has been defined on line 3. So the "classes" directory will be deleted.
- Create target
- Once the clean task has completed creates the classes folder again, we can be sure this folder is empty.
- Compile target
- Compiles all the files inside the src folder ("src.dir" variable, line 3) and puts the generated classes inside the folder classes.
- Jar target
- Packs the compiled classes into JAR files. Sets SimpleTest as the main-class in the MANIFEST.MF file (click here to read more info about this topic).
- Run Target
- Runs the jar's main class, the attribute fork="true" will force to use a new JVM instance to run this class, and do not reuse the same one used by Ant.
The targets defined on the build.xml file have been executed as expected, following the dependencies of each target. Inside the src folder there were 3 Java files: SimpleTest.java, MessagePrinter1.java and MessagePrinter2.java, these files have been compiled and the generated class files have been packed into a jar file. Finally, all of them have printed his own message as seen in the terminal above (This works!, Message printer 1., Message printer 2.).
~/lab/workspace/AntTestProject$ ant Buildfile: build.xml clean: create: [mkdir] Created dir: /home/me/lab/workspace/AntTestProject/classes compile: [javac] Compiling 3 source files to /home/me/lab/workspace/AntTestProject/classes jar: [jar] Building jar: /home/me/lab/workspace/AntTestProject/test.jar run: [java] This works! [java] Message printer 1. [java] Message printer 2. main: BUILD SUCCESSFUL Total time: 1 second ~/lab/workspace/AntTestProject$Terminal
Test filesFiles used during the test. Click on the file to download it (do not use "Save Link As...").
- AntTestProject.tar.gz - Java source files and build.xml Ant script file.