{"id":275,"date":"2026-10-13T07:11:09","date_gmt":"2026-10-13T07:11:09","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2010\/07\/22\/sample-ant-clean-prepare-and-compile-tasks\/"},"modified":"2026-10-13T07:11:10","modified_gmt":"2026-10-13T07:11:10","slug":"sample-ant-clean-prepare-and-compile-tasks","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/sample-ant-clean-prepare-and-compile-tasks\/","title":{"rendered":"Apache Ant clean, prepare, and compile Targets: A Practical build.xml Walkthrough"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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 <strong>clean \u2192 prepare \u2192 compile<\/strong> flow works, how to run it, and what to watch for in real projects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Originally published on 2010-07-22; fully revised and updated.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2010\/07\/sample-ant-clean-prepare-an.png\" alt=\"Sample Ant clean, prepare, and compile tasks\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What the clean, prepare, and compile targets do<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This pattern separates build responsibilities into predictable stages:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>clean<\/strong>: removes previously generated output.<\/li><li><strong>prepare<\/strong>: recreates required directory structure.<\/li><li><strong>compile<\/strong>: compiles application and test sources using a shared classpath.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Because Ant supports <strong>target dependencies<\/strong>, running <code>compile<\/code> automatically triggers <code>prepare<\/code>, which in turn triggers <code>clean<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Build dependency workflow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The dependency chain below reflects the original logic and execution order.<\/p>\n\n\n\n<pre class=\"mermaid\">\nflowchart TD\n    A[Run ant compile] --> B[clean target]\n    B --> C[Delete output and temp directories]\n    C --> D[prepare target]\n    D --> E[Create build directories]\n    E --> F[compile target]\n    F --> G[Compile src.dir sources]\n    G --> H[Compile src.tests.dir subset]\n    H --> I[Classes ready in temp.dir.classes]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Complete build.xml example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">project<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"sample-build\"<\/span> <span class=\"hljs-attr\">default<\/span>=<span class=\"hljs-string\">\"compile\"<\/span> <span class=\"hljs-attr\">basedir<\/span>=<span class=\"hljs-string\">\".\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">property<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"src.dir\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"src\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">property<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"src.tests.dir\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"src-tests\"<\/span> \/&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">property<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"dest.dir\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"build\/dist\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">property<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"temp.dir\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"build\/tmp\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">property<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"temp.dir.lib\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"${temp.dir}\/lib\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">property<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"temp.dir.meta-inf\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"${temp.dir}\/META-INF\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">property<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"temp.dir.web-inf\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"${temp.dir}\/WEB-INF\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">property<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"temp.dir.classes\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"${temp.dir}\/classes\"<\/span> \/&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">property<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"lib.dir\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"lib\"<\/span> \/&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">path<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"build.class.path\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">fileset<\/span> <span class=\"hljs-attr\">dir<\/span>=<span class=\"hljs-string\">\"${lib.dir}\"<\/span> <span class=\"hljs-attr\">includes<\/span>=<span class=\"hljs-string\">\"**\/*.jar\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">path<\/span>&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">target<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"clean\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">echo<\/span>&gt;<\/span>=== CLEAN ===<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">echo<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">delete<\/span> <span class=\"hljs-attr\">failonerror<\/span>=<span class=\"hljs-string\">\"false\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">fileset<\/span> <span class=\"hljs-attr\">dir<\/span>=<span class=\"hljs-string\">\"${dest.dir}\"<\/span> <span class=\"hljs-attr\">includes<\/span>=<span class=\"hljs-string\">\"**\/*\"<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">delete<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">delete<\/span> <span class=\"hljs-attr\">dir<\/span>=<span class=\"hljs-string\">\"${temp.dir}\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">target<\/span>&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">target<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"prepare\"<\/span> <span class=\"hljs-attr\">depends<\/span>=<span class=\"hljs-string\">\"clean\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">echo<\/span>&gt;<\/span>=== PREPARE ===<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">echo<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mkdir<\/span> <span class=\"hljs-attr\">dir<\/span>=<span class=\"hljs-string\">\"${dest.dir}\"<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mkdir<\/span> <span class=\"hljs-attr\">dir<\/span>=<span class=\"hljs-string\">\"${temp.dir}\"<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mkdir<\/span> <span class=\"hljs-attr\">dir<\/span>=<span class=\"hljs-string\">\"${temp.dir.lib}\"<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mkdir<\/span> <span class=\"hljs-attr\">dir<\/span>=<span class=\"hljs-string\">\"${temp.dir.meta-inf}\"<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mkdir<\/span> <span class=\"hljs-attr\">dir<\/span>=<span class=\"hljs-string\">\"${temp.dir.web-inf}\"<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">mkdir<\/span> <span class=\"hljs-attr\">dir<\/span>=<span class=\"hljs-string\">\"${temp.dir.classes}\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">target<\/span>&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">target<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"compile\"<\/span> <span class=\"hljs-attr\">depends<\/span>=<span class=\"hljs-string\">\"prepare\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">echo<\/span>&gt;<\/span>=== COMPILE ===<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">echo<\/span>&gt;<\/span>\n\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">echo<\/span>&gt;<\/span>Compiling ${src.dir} files ...<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">echo<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">javac<\/span> <span class=\"hljs-attr\">debug<\/span>=<span class=\"hljs-string\">\"on\"<\/span> <span class=\"hljs-attr\">srcdir<\/span>=<span class=\"hljs-string\">\"${src.dir}\"<\/span> <span class=\"hljs-attr\">destdir<\/span>=<span class=\"hljs-string\">\"${temp.dir.classes}\"<\/span> <span class=\"hljs-attr\">includes<\/span>=<span class=\"hljs-string\">\"**\/*.java\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">classpath<\/span> <span class=\"hljs-attr\">refid<\/span>=<span class=\"hljs-string\">\"build.class.path\"<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">javac<\/span>&gt;<\/span>\n\n        <span class=\"hljs-comment\">&lt;!-- Compile selected test sources --&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">echo<\/span>&gt;<\/span>Compiling ${src.tests.dir} files ...<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">echo<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">javac<\/span> <span class=\"hljs-attr\">debug<\/span>=<span class=\"hljs-string\">\"on\"<\/span> <span class=\"hljs-attr\">srcdir<\/span>=<span class=\"hljs-string\">\"${src.tests.dir}\"<\/span> <span class=\"hljs-attr\">destdir<\/span>=<span class=\"hljs-string\">\"${temp.dir.classes}\"<\/span> <span class=\"hljs-attr\">includes<\/span>=<span class=\"hljs-string\">\"com\/**\/*.java\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">classpath<\/span> <span class=\"hljs-attr\">refid<\/span>=<span class=\"hljs-string\">\"build.class.path\"<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">javac<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">target<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">project<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Target comparison<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Target<\/th><th>Depends on<\/th><th>Main purpose<\/th><th>Key operations<\/th><\/tr><\/thead><tbody><tr><td>clean<\/td><td>None<\/td><td>Remove prior build output<\/td><td>Delete <code>${dest.dir}<\/code> contents and <code>${temp.dir}<\/code><\/td><\/tr><tr><td>prepare<\/td><td>clean<\/td><td>Create a known directory layout<\/td><td>Create destination, temp, classes, and metadata folders<\/td><\/tr><tr><td>compile<\/td><td>prepare<\/td><td>Compile source code<\/td><td>Run <code>javac<\/code> for main and selected test sources using <code>build.class.path<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to run these targets<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">From the directory containing <code>build.xml<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs language-bash\">ant clean\nant prepare\nant compile<\/code><\/span><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In day-to-day use, <code>ant compile<\/code> is usually enough because dependencies execute upstream targets automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Keep target responsibilities narrow: cleanup, setup, and compilation should stay separate.<\/li><li>Prefer explicit include patterns like <code>**\/*.java<\/code> for predictable compilation scope.<\/li><li>Define all referenced properties and path IDs in one place near the top of <code>build.xml<\/code>.<\/li><li>Be intentional about test output location; compiling tests into the same classes directory can blur boundaries.<\/li><li>Use dependency chains instead of repeated commands so the build remains deterministic.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to structure a simple Ant build pipeline using clean, prepare, and compile targets with explicit dependencies. This revision includes a complete build.xml example, execution commands, and a workflow diagram.<\/p>\n","protected":false},"author":1,"featured_media":4415,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[12],"tags":[4227,3827,4226,4220,683,91,4221,3420,4225,519,1122,4222,938,4218,4224,4228,4223],"class_list":["post-275","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache-ant","tag-and-compile-example","tag-ant-build","tag-ant-clean","tag-ant-example","tag-apache-ant","tag-build","tag-clean","tag-compile","tag-echo","tag-example","tag-guide","tag-prepare","tag-reference","tag-sample","tag-scipt","tag-simple-ant-example","tag-task"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=275"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/275\/revisions"}],"predecessor-version":[{"id":79104,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/275\/revisions\/79104"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/4415"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}