Advertisment

Roll your Java Apps with Apache Ant

author-image
PCQ Bureau
New Update

A software developer is always interested in concentrating more on business logic rather than spending time on configuring and deploying applications on different platforms. J2EE applications have become smarter, so have their deployment procedures. Gone are those days when you had to write different build files for different platforms. Thanks to build tools such as Ant now application deployment can be handled easily in a platform-independent way, so that you can concentrate more on business logic. This article introduces you to the powerful yet, simple XML-based scripting tool that can automate the process of compilation, deployment and configuration of your applications. We will first compare Ant with other build tools, and then, guide you through the process of compilation and deployment using Ant. 

Advertisment

What is Ant?

Direct Hit!
Applies to:

Java developers
USP: A single build file for different platforms
Primary Link:

http://ant.apache.org 
Google keywords:

java build tool 

Ant, from the Jakarta Apache Project, is a platform independent Java-based build tool. Ant started off as an internal component of Tomcat, the servlet container that is used in the reference implementation for the Java Servlet and JSP (Java Server Pages) technology. With Ant (which stands for Another Neat Tool), you can write a single build file that operates consistently on any Java platform. Ant is an XML-based scripting tool. The Ant build file must be a well-formed XML document. Soon after its release, the usefulness of Ant was recognized and its usage spread throughout the other Jakarta subprojects. It was finally made into a Jakarta subproject of its own, with the first independent release being made in July of 2000. Since then, Ant has grown and become the de facto standard for building open-source Java projects.

Advertisment

Why Use Ant?



Build tools have been around for years and they are used to transform source code and other input files into an executable form, thus, automating the build process of applications. But, these tools are usually tied very closely to the underlying operating system. Traditional projects in C or C++ often use the 'make tool' for this purpose. 

Some Ant takeaways
  • Open source
  • Platform independent
  • Simple and powerful
  • Extensible
  • Pure Java based
  • Follows XML standards
  • Easy integration
  • Automate builds
  • Reduced dependency

While most build tools are capable of building Java applications, they do not provide OS independence, nor do they closely integrate with Java. Ant follows the WORA (Write Once Run Anywhere) approach wherein you write the build script once and it is capable of running on any OS. The conventional procedure for application deployment will involve development of small scripts in platform-specific languages. For example, on an NT machine the developer would first create a batch file that performs the compilation tasks and then runs the deployment. And, when you switch on to some other OS (say, Linux) then you have to rewrite the scripts. The real benefit of Ant is that it's platform independent, therefore, freeing the developer from the toiling efforts of creating different scripts for different OSs. This, in turn, helps in quick migration. Secondly, Ant is extensible; you can extend it to define your own custom tasks. Ant is implemented in the Java language and it can be easily integrated with your favorite IDE. Finally, Ant scores over jmk (make in Java), another Java-based build tool by not depending on external programs for development and file management. 

Advertisment

Development Environment



The following is the layout of the sample application directory structure.

src/jsp -This directory contains the JSP views.



src/html -This directory hosts static contents. 


src/images -All image files of the project are located 


in this directory.


Build -Ant creates this directory during the 


initialization process. All the compiled 


CLASS files would be placed here. 


Dist -This directory would host the WAR file, 


ready for distribution. 


Conf -This directory contains a property file. 







Install and configure Ant
  • Download the latest version of Ant from

    http://ant.apache.org
  • Add the Ant bin directory to your PATH
  • Set the ANT_HOME environment variable to your Ant root directory (Please make sure that the JAVA_HOME environment is already set)
Advertisment

The property file contains information that may change between different development environments within a given project. We have defined a property named CAT_HOME which points to our Tomcat installation directory.

Compiling and deploying using Ant



The real power of Ant lies in its ability to do an incremental build. Build tools like Ant bridge the gap between development and integration. Ant's advantage comes to the fore when it is used to automate all of the aspects of compiling, archiving and deploying a Web application. Now, we will show you how to compile your code, move the bytecode to the desired location, put everything into a WAR file and finally deploy it to Tomcat, using Ant.

The default name of an Ant build script is 'build.xml'. The build.xml should be a well-formatted XML file that describes the build and deployment process of your application or component. The following code snippet describes the build script.

Advertisment


































destdir="${build}" />
















webxml="src/myapp.xml">


































file="${dist}/${MyProj}.war" />






















How it works



Naming project, base directory and default target:
Every Ant build file contains one project. The attributes on the project tag are the name of the project, the default target to run when no target is specified on the command line, and the base directory to use for locating files. Each project can have only one 'basedir' attribute. Ant works best when relative directories are used.

Advertisment

Running the build script is easy. It can be done by specifying 'ant' on the command prompt as shown

Global properties: A property is a simple name and value pair that allows you to logically refer an entity. Ant allows the use of properties to save values. In this section we create a series of properties that will represent the various directories we will be using in our script. For example, if you want to refer the 'source' variable later in the Ant script, you could simply use the following syntax to alert Ant to obtain the value for this tag: ${source}. This allows making changes only at one place. 

Targets and dependencies: The target tags are used to specify the tasks to be performed. All commands within the target tag are executed in the order specified. Using 'depends', you can stack the Ant tasks so that a dependent task is not initiated until the task that it depends on is completed. In our example, the 'war' task will not start until the 'compile' task has completed.

Advertisment

Compilation: The javac built-in Ant task is used to compile the servlets and JavaBeans code. The javac command requires a source directory, which is the input location of the Java files, and a destination directory that indicates where the compiled bytecode is to be stored. Ant will automatically place the class files in the correct structure based on their package. It will also skip compiling any Java file that has already been compiled and has not been changed.

Building a WAR: The next target is named 'war' and depends upon the successful completion of the 'compile' target. The war tag will create a WAR (Web ARchive) file. The destFile attribute determines the name of the WAR file. The DD (Deployment Descriptor) of your Web application is specified by the webxml attribute. Separate fileset tags specify the files that are included in the WAR file.

Ant clean up is another task that you can perform just by keying in 'ant clean' on the command prompt

Deployment: The 'deploy' target will perform the copy operation to move the WAR file from the development directory to the deployment directory (Tomcat's webapps directory). Note that here we have used a properties file to get a reference to the webapps directory of the servlet container. Alternatively you can also use built-in 'property' task, as shown below.





Clean up: As a good practice, you should consider creating a 'clean' target for your build script. The clean target would be responsible for removing any files generated by the build.xml file.

Running Ant



Ant is a command-line build tool, which executes Java code behind the scenes. With Ant, you can execute a specific target in the Ant build file by explicitly specifying it on the command line. Running the build script is as easy as specifying 'ant' on the command prompt. Now we can clean up our directories by running 'ant clean'.

Note that if you are running an Ant build file with a different name, then launch it with 'buildfile' option as follows.

ant —buildfile myscript.xml

Tool of choice



Ant has evolved into the build tool of choice for automating Java projects that can free developers from cross-platform deployment blues. But there are some misconceptions related with Ant. Let us address some of them. First of all, Ant doesn't replace the Java IDEs. As developers you still have to use your favorite IDE to write code and deployment descriptors. Ant comes into play when a component is ready for integration and deployment. With Ant, you still have to do all of the configuring, but then Ant automates the process. Ant allows you to define your packaging, build, and deployment processes with ease. Secondly, Ant is not a full-fledged programming language, although it supports most common tasks such as java, javac, jar, war and several other file systems tasks, which are available out of the box. But, the advantage of Ant is that it is extensible. 

As your project grows more complex, you can split your build script into smaller pieces by having high-level build files that call out to other build files to perform specific task. This can be achieved by using the ant predefined task. This helps you to chain your builds together and have a fine gained control over your project. 

Kunal Jaggi

Advertisment