Preparing to Deploy

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.

Deleting Existing Files

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

Attribute

Description

Required

Default

defaultexcludes

Specifies if you want to use default excludes. Set to yes/no.

No

Default excludes are used.

dir

Specifies the name of a directory to delete. All its files and subdirectories will be deleted.

At least one of file or dir (unless a fileset element is specified).

 

excludes

Deprecated. Use a fileset element. Specifies the patterns matching files to exclude, as a comma- or space-separated list.

No

No files (except default excludes) are excluded.

excludesfile

Deprecated. Use a fileset element. Specifies the name of a file where each line is a pattern matching files to exclude.

No

 

failonerror

Specifies if you want an error to stop the build. Only used if if quiet is false.

No

true

file

Specifies the file you want to delete.

At least one of file or dir (unless a fileset element is specified).

 

includeEmptyDirs

Specifies if you want to delete empty directories when using file sets.

No

false

includes

Deprecated. Use a nested fileset element. Specifies the patterns matching files to include, as a comma- or space-separated list.

No

All files are included.

includesfile

Deprecated. Use a nested fileset element. Specifies the name of a file where each line is a pattern matching files to include.

No

 

quiet

Suppresses most diagnostic messages.

No

 

verbose

Specifies that you want to show the name of each deleted file (true/false).

No

false

The delete task can contain nested fileset elements.

Tip

Here's something you might not have expected: empty directories are not deleted by default. To remove empty directories, use the includeEmptyDirs attribute.

Creating New Directories

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.

Table 4-10. The mkdir task's attributes

Attribute

Description

Required

dir

Specifies the directory you want to create

Yes

Want to create a directory? Just do it:

<mkdir dir="${dist}"/>

Tip

Just realized that you've asked mkdir to create a directory whose parent directories don't exist? That's not a problem since mkdir creates parent directories as needed.

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.