Ant supports several tasks for setting up a deployment
environment, such as delete
and
mkdir
. Both these tasks can be used
locally or on a network to set up the directory structure you need to
deploy applications.
Tip
If you want to create and delete directories remotely, take a
look at the ftp
task, coming up
later in this chapter.
When deploying, delete
is great to clean up a previous
installation or to clean deployment directories before installing.
This task deletes a single file, a directory and all its files and
subdirectories, or a set of files specified by one or more
FileSets.
Using this task, you can delete a single file:
<delete file="/lib/Project.jar"/>
Or you can delete an entire directory, including all files and subdirectories:
<delete dir="${dist}"/>
You can use filesets:
<delete includeEmptyDirs="true"> <fileset dir="${dist}"/> </delete>
You've seen delete
at work in
various places throughout the book, as in the build file in the
input folder for Chapter 3s code (repeated in Example 4-2), where the user is
asked for confirmation before deleting anything.
Example 4-2. Using the delete task (ch03/input/build.xml)
<?xml version="1.0" ?> <project default="main"> <property name="message" value="Building the .jar file." /> <property name="src" location="source" /> <property name="output" location="bin" /> <target name="main" depends="init, compile, compress"> <echo> ${message} </echo> </target> <target name="init"> <input message="Deleting bin directory OK?" validargs="y,n" addproperty="do.delete" /> <condition property="do.abort"> <equals arg1="n" arg2="${do.delete}"/> </condition> <fail if="do.abort">Build aborted.</fail> <delete dir="${output}" /> <mkdir dir="${output}" /> </target> <target name="compile"> <javac srcdir="${src}" destdir="${output}" /> </target> <target name="compress"> <jar destfile="${output}/Project.jar" basedir="${output}" includes="*.class" /> </target> </project>
Tip
If you use this task to delete temporary files created by
editors or other software and it doesn't work, try setting the
defaultexcludes
attribute to
no
.
You can see the attributes of this task in Table 4-9.
Tip
The includes
, includesfile
, exclude
, and excludesfile
attributes are deprecated and
are being replaced by fileset
.
This makes me suspect that other tasks will follow this same
pattern.
Table 4-9. The delete task's attributes
Want to create the directory structure for local or
network deployment? Use mkdir
. This
one's so important that you've seen it in use since Chapter 1. And it's easy to use with
only one attribute, as you can see in Table 4-10.
Want to create a directory? Just do it:
<mkdir dir="${dist}"/>
Get Ant: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.