This is the third part of the so called Ant series I have been writing in this blog, about Apache Ant. This post is mainly about building and running a Java program using Ant. In the example I'm using here, its a Hello World program in simple Java, but most of these basic Ant steps would be same for any other Java program you might run.
compile: [mkdir] Created dir: /opt/ant-hw/build/classes [javac] Warning: HelloWorld.java modified in the future. [javac] Compiling 1 source file to /opt/ant-hw/build/classes
jar: [mkdir] Created dir: /opt/ant-hw/build/jar [jar] Building jar: /opt/ant-hw/build/jar/HelloWorld.jar
run: [java] Hello World BUILD SUCCESSFUL Total time: 4 seconds
Enhance the build file
Now we have a working buildfile we could do some enhancements: many time you are referencing the same directories, main-class and jar-name are hard coded, and while invocation you have to remember the right order of build steps.
As I have worked on Ant before sometime for a project and I had few of my own documents for reference, related to installing and using Apache Ant. I thought of sharing those via this blog as it might be useful to someone out there, or at-least would be a backup of my doc in the Blogger servers. ;)
In this series of Ant, I put and introductory post which is followed by a Hello World post, and then I got a couple of IMs asking how could one come to Hello World before an installation post. (Seems thats the standard way of tutorial, so I said I'm not writing any tutorials, but thought of posting this now... :) ... ). System Requirements for Ant
Ant has been used successfully on many platforms, including Linux, MacOS X, Windows XP and Unix. To build and use Ant, you must have a JAXP-compliant XML parser installed and available on your classpath, such as Xerces.
For the current version of Ant, you will also need a JDK installed on your system, version 1.2 or later required, 1.5 or later strongly recommended. The later the version of Java, the more Ant tasks you get.
Note: If a JDK is not present, only the JRE runtime, then many tasks will not work.
Building Ant
Obtaining Ant
To build Ant from source, you can either install the Ant source distribution or checkout the Ant module from SVN. I obtained the Ant source version and placed it at /opt/
Once you have obtained the source, change into the installation directory and run,
[root@nimal opt]# tar -xzvf apache-ant-1.7.0-src.tar.gz
This will creat Set the JAVA_HOME environment variable to the directory where the JDK is installed. See Installing Ant for examples on how to do this for your operating system.
Note: The bootstrap process of Ant requires a greedy compiler like Sun's javac or jikes. It does not work with gcj or kjc.
Make sure you have downloaded any auxiliary jars required to build tasks you are interested in. These should be added to the lib/optional directory of the source tree. See Library Dependencies for a list of JAR requirements for various features. Note that this will make the auxiliary JAR available for the building of Ant only. For running Ant you will still need to make the JARs available as described under Installing Ant.
build -Ddist.dir=<directory_to_contain_Ant_distribution> dist (Windows) sh build.sh -Ddist.dir=<directory_to_contain_Ant_distribution> dist (Unix)
[root@nimal opt]# cd /opt/apache-ant-1.7.0/ [root@nimal apache-ant-1.7.0]# sh build.sh -Ddist.dir=/usr/local/ant/ dist Buildfile: build.xml
dist:
prepare:
check_for_optional_packages:
build: [copy] Copying 2 files to /opt/apache-ant-1.7.0/build/classes
jars: [jar] Building jar: /opt/apache-ant-1.7.0/build/lib/ant.jar
. . .
compile-tests:
test-jar:
dist-lite: [mkdir] Created dir: /usr/local/ant [mkdir] Created dir: /usr/local/ant/bin [mkdir] Created dir: /usr/local/ant/lib [copy] Copying 8 files to /usr/local/ant/lib [copy] Copying 2 files to /usr/local/ant/lib [copy] Copying 13 files to /usr/local/ant/bin
javadoc_check:
javadocs: [mkdir] Created dir: /opt/apache-ant-1.7.0/build/javadocs [javadoc] Generating Javadoc
. . .
[javadoc] 114 warnings
dist_javadocs: [mkdir] Created dir: /usr/local/ant/docs/manual/api [copy] Copying 1180 files to /usr/local/ant/docs/manual/api
internal_dist: [mkdir] Created dir: /usr/local/ant/etc [copy] Copying 1 file to /usr/local/ant/lib [copy] Copying 1 file to /usr/local/ant/lib [copy] Copying 25 files to /usr/local/ant/lib [copy] Copying 1 file to /usr/local/ant/lib [copy] Copying 259 files to /usr/local/ant/docs [copy] Copying 33 files to /usr/local/ant/docs [copy] Copying 11 files to /usr/local/ant [copy] Copying 15 files to /usr/local/ant/etc
BUILD SUCCESSFUL Total time: 59 seconds
This will create a binary distribution of Ant in the directory you specified.
The above action does the following:
If necessary it will bootstrap the Ant code. Bootstrapping involves the manual compilation of enough Ant code to be able to run Ant. The bootstrapped Ant is used for the remainder of the build steps.
Invokes the bootstrapped Ant with the parameters passed to the build script. In this case, these parameters define an Ant property value and specify the "dist" target in Ant's own build.xml file.
Create the ant.jar and ant-launcher.jar JAR files
Create optional JARs for which the build had the relevant libraries. If a particular library is missing from ANT_HOME/lib/optional, then the matching ant- JAR file will not be created. For example, ant-junit.jar is only built if there is a junit.jar in the optional directory.
Other ways to build and install
On most occasions you will not need to explicitly bootstrap Ant since the build scripts do that for you. If however, the build file you are using makes use of features not yet compiled into the bootstrapped Ant, you will need to manually bootstrap. Run bootstrap.bat (Windows) or bootstrap.sh (UNIX) to build a new bootstrap version of Ant. If you wish to install the build into the current ANT_HOME directory, you can use:
build install (Windows) sh build.sh install (Unix)
You can avoid the lengthy Javadoc step, if desired, with:
build install-lite (Windows) sh build.sh install-lite (Unix)
This will only install the bin and lib directories.
Both the install and install-lite targets will overwrite the current Ant version in ANT_HOME.
Checking the Installation
Now we have built and installed Ant, and we can check if everything works properly, as shown below.
[root@nimal ~]# which ant /usr/local/ant/bin/ant [root@nimal ~]# ant -version Apache Ant version 1.7.0 compiled on November 13 2007
This is just a brief document related to Ant installation which I did on a RHEL4 server. I think this should work on other environments as well, but I'm not sure about that. If someone of you try this using my doc and if you are successful just let me know via a comment. Also if you have any doubts you can ask me, but I can assure answers, if-and-only-if its under my intellectual capacity :).
The first line in the file tells the system that this is an XML file. The next line tells ant that we have a project named "My Project" and that it has a default target called "hello". Note that the file must always start with a <project> and end with a </project>. The central three lines are the one and only target in the file. They give the target a name and the target has just one task called "echo"
You can now open a shell and type "ant". The default task is a required attribute of the project. Each target must have a name.
Output of Hello World
[root@nimal ant-tests]# ant Buildfile: build.xml
hello: [echo] Hello World!
BUILD SUCCESSFUL Total time: 1 second [root@nimal ant-tests]#
Variations
The following variations of echo will also give similar output.