Wednesday, February 27, 2008

Ant: Java Hello World in Ant

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.

The previous posts on Ant can be found here:
  1. Apache Ant
  2. Ant: Installation
  3. Ant: Hello World in Ant

Java Program

Create the following simple program and save it as HelloWorld.java in a sub folder src:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

Ant Script

Use a text editor to create a file called build.xml and place it in the main folder:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Run Test" default="run" basedir=".">

<target name="clean"> <delete dir="build"/>
</target>

<target name="compile" depends="clean">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/> </target>

<target name="jar" depends="compile">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar"
basedir="build/classes"> <manifest>
<attribute name="Main-Class" value="HelloWorld"/>
</manifest>
</jar>
</target>

<target name="run" depends="jar"> <java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>

</project>

Output

Run ant in command line:

[root@nimal ant-hw]# ant
Buildfile: build.xml

clean:
[delete] Deleting directory /opt/ant-hw/build

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.

<project name="HelloWorld" basedir="." default="main">

<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>

<target name="compile">
<mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>

<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar"
basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/> </manifest>
</jar>
</target>

<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/> </target>

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,run"/>

</project>
Feedback and questions are welcome via comment or you can email me at talkout AT SPAMFREE gmail DOT com
 
 

Saturday, February 23, 2008

Ant: Installation

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.

Note: I did this installation on a HP server running Red Hat Enterprise Linux WS Version 4 Update 4.

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.

In our case we need the JUnit jar.

Environment Settings

Linux/Unix (bash)

Assume Ant is installed in /usr/local/ant. The following sets up the environment:

export ANT_HOME=/usr/local/ant export JAVA_HOME=/usr/java/jdk1.6.0/ export PATH=${PATH}:${ANT_HOME}/bin

The above can be set by editing the .bashrc file.

[root@nimal ~]# vi ~/.

Once this file is opened it will look like this.

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

Append these lines below the end of the if clause below fi

export ANT_HOME=/usr/local/ant/
export JAVA_HOME=/usr/java/jdk1.6.0/
export PATH=${PATH}:${ANT_HOME}/bin

And the final file should look like this; save and close the file.

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

export ANT_HOME=/usr/local/ant/
export JAVA_HOME=/usr/java/jdk1.6.0/
export PATH=${PATH}:${ANT_HOME}/bin
Normal build and install

Your are now ready to build 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 :).

Reference: http://ant.apache.org/manual/install.html
 

Monday, February 18, 2008

Ant: Hello World in Ant

This is a simple reference of my, which might be helpful to someone after installing Ant and do some playing.

Use a text editor to create a file called build.xml:
<?xml version="1.0"?>
<project name="My Project" default="hello">
<target name="hello">
<echo>Hello World!</echo>

</target>
</project>

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.

<echo message="Hello There!"></echo>
<echo message="Hello There!"/>