Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Automate your build process using Java and Ant - JavaWorld October 2000

Automate your build process using Java and Ant - JavaWorld October 2000

Tutorial Details:

Automate your build process using Java and Ant
Automate your build process using Java and Ant
By: By Michael Cymerman
Introducing the powerful XML-based scripting tool, Ant
defined process is one of the most necessary but often least-used tools in software development. It is by nature an overhead task that accompanies a development effort. A defined build process ensures that the software in your development project is built in the exact same manner each time a build is executed. As the build process becomes more complex -- for example, with EJB builds or additional tasks -- it becomes more necessary to achieve such standardization. You should establish, document, and automate the exact series of steps as much as possible.
Why do I need a defined build process?
A defined build process is an essential part of any development cycle because it helps close the gap between the development, integration, test, and production environments. A build process alone will speed the migration of software from one environment to another. It also removes many issues related to compilation, classpath, or properties that cost many projects time and money.
What is Ant?
Ant is a platform-independent scripting tool that lets you construct your build scripts in much the same fashion as the "make" tool in C or C++. You can use a large number of built-in tasks in Ant without any customization. Some of the most important tasks are shown in the following table but explained in more detail in the example that follows.
Here are some useful commands that are built in the Ant distribution.
Command
Description
Ant
Used to execute another ant process from within the current one.
Copydir
Used to copy an entire directory.
Copyfile
Used to copy a single file.
Cvs
Handles packages/modules retrieved from a CVS repository.
Delete
Deletes either a single file or all files in a specified directory and its sub-directories.
Deltree
Deletes a directory with all its files and subdirectories.
Exec
Executes a system command. When the os attribute is specified, then the command is only executed when Ant is run on one of the specified operating systems.
Get
Gets a file from an URL.
Jar
Jars a set of files.
Java
Executes a Java class within the running (Ant) VM or forks another VM if specified.
Javac
Compiles a source tree within the running (Ant) VM.
Javadoc/Javadoc2
Generates code documentation using the javadoc tool.
Mkdir
Makes a directory.
Property
Sets a property (by name and value), or set of properties (from file or resource) in the project.
Rmic
Runs the rmic compiler for a certain class.
Tstamp
Sets the DSTAMP, TSTAMP, and TODAY properties in the current project.
Style
Processes a set of documents via XSLT.
While other tools are available for doing software builds, Ant is easy to use and can be mastered within minutes. In addition, Ant lets you create expanded functionality by extending some of its classes. I will show this expansion in a following example.
What do I need to use Ant?
You must install three components on your machine to run Ant: JDK, XML parser, and Ant (see Resources for links).
In many cases, the XML parser is part of the lib files distributed with the Servlet runner or the Web Server. If not, the free XML parser from java.sun.com is sufficient.
Ant installation consists of downloading the files, adding the class libraries to the classpath, and adding the Ant binaries to the path.
Example scenario
This example scenario should help show you the value of Ant and provide insight into its benefits and how you can use it.
Because a large amount of the current Java development is focused on server-side Java, I have chosen a server-side application for the example. Developers working on server-side Java applications are typically interested in the compilation of servlets, deployment of JSP files, and deployment of HTML files, configuration files, or images.
A common scheme for doing this build would involve the development of small scripts in platform-specific languages based on the server's operating system. For example, a developer working on an NT machine could create a batch file that performs the compilation tasks and then runs the deployment. However, if the production environment had Unix or Linux, the developer would have to rewrite the script, ensuring that the scripts were in sync.
OK, show me how this works
So, I've hopefully convinced you of the need to use Ant and shown how simple it is to install. Now I'll show you how simple Ant is to use by stepping through an example that performs simple compilation and deployment.
Simple build process with Ant (simple.xml)





















There's a lot to explain in the above example. First, you should understand the structure of the simple.xml file. It is a well-formatted XML file containing a project entity that is comprised of several target entities.
The first line contains information about the overall project that is to be built.

The most important elements of the project line are the default and the basedir .
The default attribute references the default target that is to be executed. Because Ant is a command-line build tool, it is possible to execute only a subset of the target steps in the Ant file. For example, I could perform the following command:
% ant -buildfile simple.xml init
That will execute the ant command and run through the simple.xml file until the init target is reached. So, in this example, the default is deploy . The Ant process invoked in the following line will run through the simple.xml file until the deploy command is reached:
% ant -buildfile simple.xml
The basedir attribute is fairly self-explanatory as it is the base directory from which the relative references contained in the build file are retrieved. Each project can have only one basedir attribute so you can choose to either include the fully qualified directory location or break the large project file into smaller project files with different basedir attributes.
The next line of interest is the target line. Two different versions are shown here:


The target element contains four attributes: name , if , unless , and depends . Ant requires the name attribute, but the other three attributes are optional.
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 the above example, the clean task will not start until the init task has completed. The depends attribute may also contain a list of comma-separated values indicating several tasks that the task in discussion depends on.
The if and unless commands let you specify commands that are to be performed either if a certain property is set or unless that property is set. The if will execute when the property value is set, and the unless will execute if the value is not set. You can use the available command to set those properties as shown in a following example, or you can set them via the command line.
The init target from the simple example contains four lines of property commands as shown here:

These property lines let you specify commonly used directories or files. A property is a simple name value pair that allows you to refer to the directory or file as a logical entity rather than a physical one.
If you wanted to reference the sourceDir variable later in the Ant file, you could simply use the following syntax to alert Ant to obtain the value for this tag: ${sourceDir} .
Two other commands present in the above buildfile are:


These commands are used to ensure that there are no extraneous files in the outputDir (or classes directory when dereferenced as mentioned above). The first command removes the entire tree contained under the outputDir . The second command creates the directory again.
The last line of major interest to the developer is the following compilation line:

The javac command requires a source directory (the input location of the .java files) and a destination directory (the output location of the .classes file). It is important to note that all directories must either exist prior to the running of the ant command or be created using the mkdir command. Ant does not create directories based upon intuition, so you must create the outputDir , using the mkdir command prior to the compilation step above.
After the compile task has completed, the deploy task will perform the copy operation to move all JSP files from the source directory to a deployment directory. By using the copydir command, you copy the entire JSP directory from one location to another. I used the copyfile command to copy a single properties file as part of the build.
While it took several lines to explain the example, it should be evident that Ant is an easy-to-use tool. Using this buildfile as a starting point, you should be able to incorporate Ant into your development effort. The ant commands shown in the above


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Automate your build process using Java and Ant - JavaWorld October 2000

View Tutorial:
Automate your build process using Java and Ant - JavaWorld October 2000

Related Tutorials:

Maven ties together tools for better code management
Maven ties together tools for better code management
 
Best tools for mobile application development
Best tools for mobile application development
 
Axis-orizing objects for SOAP
Axis-orizing objects for SOAP
 
Very interesting article
Very interesting article
 
Overcome J2SE 1.3-1.4 incompatibilities
Overcome J2SE 1.3-1.4 incompatibilities
 
Bug patrol
Bug patrol
 
Top 15 Ant Best Practices
Top 15 Ant Best Practices Ant, building and deploying Java applications required a hodgepodge of platform-specific scripts, makefiles, proprietary IDEs, or manual processes. Now, nearly every open source Java project uses Ant. A great number of companie
 
Flexible User and Environment Ant Configuration
Flexible User and Environment Ant Configuration The de facto standard for building, packaging, and deploying Java applications is Apache Ant. Small differences in developers\' environments or preferences may cause problems with some Ant tasks that invo
 
Writing Ant Tasks
Writing Ant Tasks A nice feature of Ant is that it is designed to allow you to add your own tasks and use them in an build. This article shows you the basics of writing an Ant task and how to get a task to work.
 
Copy FQN
Introducing CopyFQN "Copy FQN" is a plugin for the Eclipse platform which adds an option to the context-menu of java-classes which copies the fully-qualified classname (com.mycompany.MyClass) to the clipboard. This is extremely useful when editing confi
 
JLAN Server v3.3
JLAN Server v3.3 JLAN Server is a high performance JavaTM based file server supporting Windows file sharing (SMB/CIFS), NFS and FTP protocols. Write your own virtual filesystems with the core server handling all protocol exchanges with the client. Incl
 
Build scripts with Groovy and Ant
Build scripts with Groovy and Ant Summary In nearly all developers' toolboxes, Ant is the standard build tool for Java applications, thanks to its open, standard, and multiplatform structure. Though it represents a great improvement in automating produc
 
Using Timers in J2EE Applications
Using Timers in J2EE Applications Job scheduling is nothing new--most enterprise applications require the scheduling of tasks and activities. For example, your application may need a timer service to run a business process once a day, or to clean up a te
 
Automate GUI tests for Swing applications
Summary Automation is necessary for frequent and consistent testing, which is the foundation of agile development. However, acceptance tests of GUI applications are not always easy to automate. This article explains a simple way of automating Java Swing
 
Building Web Application With Ant and Deploying on Jboss 3.0
This lesson shows you how to build you web application and install on the Jboss 3.0 application server. After the completion of this lesson you will be able to compile, assemble and deploy your J2EE application on Jboss 3.0 application server
 
Develop MIDlets using the J2ME MIDP Development for NetBeans IDE 4.0
This release integrates with the J2ME Wireless Toolkit 2.2 to create a powerful environment for developing MIDP 2.0 applications.
 
10 Minutes Guide to Ant
10 Minutes Guide to Ant 10 Minutes Guide to Ant Previous Tutorial Index Next Introduction Well for the next 10 minutes get ready to devote to the ant guide. This will make some sence to the ant. Ant is a free tool under GNU Licence and is
 
Building Web Application With Ant and Deploying on Jboss 3.0
Building Web Application With Ant and Deploying on Jboss 3.0 Building Web Application With Ant and Deploying on Jboss 3.0 Previous Tutorial Index Next In this lesson I will show you how to build you web application and install on the Jboss 3.0
 
Welcome to the Jboss 3.0 Tutorial
Welcome to the Jboss 3.0 Tutorial Welcome to the Jboss 3.0 Tutorial 10 Minutes Guide to Ant Comprehensive description of Ant with example. Building Web Application With Ant and Deploying on Jboss 3.0 This lesson shows you how to build you web
 
Beginner to advance guide to the Apache Struts
Beginner to advance guide to the Apache Struts The Complete Apache Struts Tutorial This complete reference of Jakarta Struts shows you how to develop Struts applications using ant and deploy on the JBoss Application Server. Ant script is provided
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.