Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Apache Ant clean, prepare, and compile Targets: A Practical build.xml Walkthrough

Apache Ant organizes Java build automation into named targets with dependency chains. This guide is for engineers maintaining legacy or lightweight Java build systems, and it shows exactly how a clean → prepare → compile flow works, how to run it, and what to watch for in real projects.

Originally published on 2010-07-22; fully revised and updated.

Sample Ant clean, prepare, and compile tasks

What the clean, prepare, and compile targets do

This pattern separates build responsibilities into predictable stages:

  • clean: removes previously generated output.
  • prepare: recreates required directory structure.
  • compile: compiles application and test sources using a shared classpath.

Because Ant supports target dependencies, running compile automatically triggers prepare, which in turn triggers clean.

Build dependency workflow

The dependency chain below reflects the original logic and execution order.

flowchart TD
    A[Run ant compile] --> B[clean target]
    B --> C[Delete output and temp directories]
    C --> D[prepare target]
    D --> E[Create build directories]
    E --> F[compile target]
    F --> G[Compile src.dir sources]
    G --> H[Compile src.tests.dir subset]
    H --> I[Classes ready in temp.dir.classes]

Complete build.xml example

The original post showed core targets only. The example below keeps that logic and adds the minimal surrounding properties and classpath so it can be used as a practical template.

<project name="sample-build" default="compile" basedir=".">
    <property name="src.dir" value="src" />
    <property name="src.tests.dir" value="src-tests" />

    <property name="dest.dir" value="build/dist" />
    <property name="temp.dir" value="build/tmp" />
    <property name="temp.dir.lib" value="${temp.dir}/lib" />
    <property name="temp.dir.meta-inf" value="${temp.dir}/META-INF" />
    <property name="temp.dir.web-inf" value="${temp.dir}/WEB-INF" />
    <property name="temp.dir.classes" value="${temp.dir}/classes" />

    <property name="lib.dir" value="lib" />

    <path id="build.class.path">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>

    <target name="clean">
        <echo>=== CLEAN ===</echo>
        <delete failonerror="false">
            <fileset dir="${dest.dir}" includes="**/*" />
        </delete>
        <delete dir="${temp.dir}" />
    </target>

    <target name="prepare" depends="clean">
        <echo>=== PREPARE ===</echo>
        <mkdir dir="${dest.dir}" />
        <mkdir dir="${temp.dir}" />
        <mkdir dir="${temp.dir.lib}" />
        <mkdir dir="${temp.dir.meta-inf}" />
        <mkdir dir="${temp.dir.web-inf}" />
        <mkdir dir="${temp.dir.classes}" />
    </target>

    <target name="compile" depends="prepare">
        <echo>=== COMPILE ===</echo>

        <echo>Compiling ${src.dir} files ...</echo>
        <javac debug="on" srcdir="${src.dir}" destdir="${temp.dir.classes}" includes="**/*.java">
            <classpath refid="build.class.path" />
        </javac>

        <!-- Compile selected test sources -->
        <echo>Compiling ${src.tests.dir} files ...</echo>
        <javac debug="on" srcdir="${src.tests.dir}" destdir="${temp.dir.classes}" includes="com/**/*.java">
            <classpath refid="build.class.path" />
        </javac>
    </target>
</project>Code language: HTML, XML (xml)

Target comparison

TargetDepends onMain purposeKey operations
cleanNoneRemove prior build outputDelete ${dest.dir} contents and ${temp.dir}
preparecleanCreate a known directory layoutCreate destination, temp, classes, and metadata folders
compileprepareCompile source codeRun javac for main and selected test sources using build.class.path

How to run these targets

From the directory containing build.xml:

ant clean
ant prepare
ant compile

In day-to-day use, ant compile is usually enough because dependencies execute upstream targets automatically.

Best practices

  • Keep target responsibilities narrow: cleanup, setup, and compilation should stay separate.
  • Prefer explicit include patterns like **/*.java for predictable compilation scope.
  • Define all referenced properties and path IDs in one place near the top of build.xml.
  • Be intentional about test output location; compiling tests into the same classes directory can blur boundaries.
  • Use dependency chains instead of repeated commands so the build remains deterministic.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Apache Ant Interview Questions Answered: Concepts, build.xml Examples, and Practical Snippets

This guide converts a long Apache Ant interview question list into clear, practical answers with working build.xml examples. It covers core Ant concepts, common tasks, debugging, and Ant vs Maven vs Make.

Read More

Apache Ant Comments and echo Task: Write Clear build.xml Files and Control Log Output

Learn the correct way to add comments in Apache Ant and when to use the echo task instead. This guide covers echo attributes, log-level behavior, and practical build.xml examples.

Read More

How to Run Ant Builds in IntelliJ IDEA: Setup, Troubleshooting, and Copy Task Patterns

This guide shows how to integrate a build.xml file with IntelliJ IDEA, run Ant targets from the IDE, and troubleshoot common environment-variable issues. It also compares practical Ant copy/fileset patterns with reusable examples.

Read More

Apache Ant Copy Task Examples: File, FileSet, Include/Exclude, and Flatten Patterns

Learn practical Apache Ant copy task patterns for real build pipelines, from single-file copies to filtered FileSets and flattened library bundles. Includes reusable XML snippets and a workflow diagram.

Read More

Apache Ant Dependency Visualization: AntScript Visualizer, Grand, and Vizant

A practical, updated guide to three classic Apache Ant visualization tools and how they help you understand target dependencies. Includes a side-by-side comparison, workflow diagram, and example commands.

Read More

Apache Ant Task Reference for DevOps: zip, concat, replaceregexp, checksum, and fail

A practical, updated reference for five widely used Apache Ant tasks: zip, concat, replaceregexp, checksum, and fail. Learn key attributes, safe defaults, and production-ready build.xml examples.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x