In this article, we will install and test Apache Ant 1.7.1 on the Ubuntu 9.10 operating system. The installation will be done within the user's home folder, check the "
Work area" post if you want to see clearly the folders structure used during the installation. Once it is installed on the system, we will test it with a tiny example to see the most basic functionalities.
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.
Environment
Required software that has already been installed and configured on the system.
Required files (~/downloads)
Downloaded files required during the installation process.
Installation steps
Step 1 - Unpack and move to the desired location
Unpack 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/ant
Terminal
Step 2 - Configure environment variables
Before 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.
# Ant environment variables
export ANT_HOME=$HOME/lab/tools/ant
export PATH=$PATH:$ANT_HOME/bin
~/.profile
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 -version
Apache Ant version 1.7.1 compiled on June 27 2008
Terminal
Test
This 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.
02 | < project name = "simpleAntProject" basedir = "." default = "main" > |
04 | < property name = "src.dir" value = "src" /> |
05 | < property name = "classes.dir" value = "classes" /> |
06 | < property name = "main.class" value = "SimpleTest" /> |
07 | < property name = "jar.name" value = "test.jar" /> |
09 | < target name = "jar" depends = "compile" description = "Creates the JAR file" > |
10 | < jar destfile = "${jar.name}" > |
11 | < fileset dir = "${classes.dir}" /> |
13 | < attribute name = "Main-Class" value = "${main.class}" /> |
18 | < target name = "compile" depends = "create" description = "Compiles all .java files" > |
19 | < javac destdir = "${classes.dir}" > |
20 | < src path = "${src.dir}" /> |
24 | < target name = "create" depends = "clean" description = "Creates the directories" > |
25 | < mkdir dir = "${classes.dir}" /> |
28 | < target name = "clean" description = "Deletes the output directories" > |
29 | < delete dir = "${classes.dir}" /> |
32 | < target name = "run" depends = "jar" description = "Runs the main-class in the JAR file" > |
33 | < java jar = "${jar.name}" fork = "true" /> |
36 | < target name = "main" depends = "clean,run" /> |
build.xml
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.
When no arguments are specified, Ant looks for a build.xml file in the current directory and, if found, uses that file as the build file and runs the target specified in the default attribute of the
project tag. To make Ant use a build file other than build.xml, use the command-line option -buildfile file, where file is the name of the build file you want to use.
~/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
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.).
Test files
Files used during the test. Click on the file to download it (do not use "Save Lin
k As...").
No comments:
Post a Comment