Ant Tasks


We used Ant throughout the book, including within Eclipse. Although we have already looked at some very useful and robust features of Ant, let's explore a few more Ant tasks here, demonstrated in our antextras.xml file. Some of these are built-in tasks, others are considered external tasks in that they require you to place one or more JAR files (libraries) in the <ant-home>/lib directory (ant-home implies the Ant's install directory; for example, C:\apache-ant-1.6.5).

As you will see from some of these tasks, Ant is one of those beautifully designed tools because of its simplicity; yet it has enormous power. It is much more than a build tool, as I will demonstrate next.

For additional information on these and other tasks not mentioned here, visit the ant.apache.org website. This site also provides information about writing your own custom Ant tasks.

CVS

As the name implies, this task enables us to work with CVS. This does require you to have the CVS executable in your path. For example, I downloaded TortoiseCVS from the tortoisecvs.org website and was able to use the cvs.exe found in this program's directory (on Linux, Unix, and Mac OS X systems, you might already have CVS prein-stalled). The following XML demonstrates how this task works; here I was able to download the Spring framework spring module using Ant:

<cvs cvsRoot=":pserver:anonymous@cvs.sourceforge.net:/cvsroot/springframework"    package="spring"     dest="/temp" />


Exec

This task enables us to run external programs; for example, the following code runs the date command:

<exec command="date"/>


Get

The Get task enables us to fetch a file using HTTP GET as demonstrated here:

<get src="/books/1/191/1/html/2/http://visualpatterns.com/comics/funny.gif"    dest="funny.gif"    verbose="true"/>


Sleep

As you might guess, this task allows us to pause processing by a specified amount of time, as demonstrated here using 2 seconds:

<sleep seconds="2"/>


FTP

This is a very powerful task because it enables us to use FTP directly from the Ant XML file. For example, while working on this book, I used it to back up my book-related files and Time Expression files to an FTP server, automatically using the Microsoft Windows Scheduled Tasks feature!

<ftp server="mirrors.kernel.org"    action="get"    remotedir="/gnu/chess"    user    password="guest@guest.com"    verbose="yes"    binary="yes">   <fileset file="README.gnuchess"/> </ftp>


FTP is an external task and requires the following files to be present in the <ant-home>/lib directory:

  • Jakarta Commons Net (http://jakarta.apache.org/commons/net/;commons-net <version>.jar)

  • Jakarta-ORO (http://jakarta.apache.org/oro/;jakarta-oro<version>.jar)

Mail

This task enables us to send mail using SMTP. It also allows us to attach files using the <fileset> element. This makes for a very powerful combination because we could use this to send emails after a build is complete, for example (particularly useful in a automated continuous build/integration environment). The following is a simple example of how this task can be used:

<mail tolist="friend@somehost.com"    subject="Hello!"    from="me@myhost.com"    mailhost="myhost.com"    user="myuserid"    password="mypassword"/>


This feature requires mail.jar (part of the JavaMail API) and activation.jar (part of the JavaBeans Activation Framework) present in the <ant-home>/lib directory. Both of these can be found on the java.sun.com website.

Tasks Galore!

I've mentioned only a few Ant tasks that I thought you might find useful. There are literally hundreds of Ant tasks available, everything from vendor-specific tasks to general ones such as Gzip, Tar, and many more. Visit the ant.apache.org website to learn about some of these tasks and how to write your own custom tasks.



Agile Java Development with Spring, Hibernate and Eclipse
Agile Java Development with Spring, Hibernate and Eclipse
ISBN: 0672328968
EAN: 2147483647
Year: 2006
Pages: 219

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net